Search code examples
javascriptmousewheel

prevent mousewheel 1s after mousewheel


I have a problem, i want like this:

$(window).on('mousewheel', function(event) {
  //prevent mousewheel in 1s
  //do something, after 1s, enable mousewheel
}

or like this: you rolled wheel many times in 1s, function in event mousewheel run 1 times. Someone help me!


Solution

  • You could do something like this:

    var mouseWheelEnabled = true;
    
    function doSomethingWithMousewheel(event){
        if(mouseWheelEnabled){
        //... your code here
    
    
        // set enabled = false;
        mouseWheelEnabled = false;
    
        setTimeout(function(){
          mouseWheelEnabled = true;
        }, 1000);
      }
    
    }
    
    $(window).on('mousewheel', doSomethingWithMousewheel);