0

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!

Neo
  • 151
  • 1
  • 1
  • 7
  • Possible duplicate of [Can someone explain the "debounce" function in Javascript](https://stackoverflow.com/questions/24004791/can-someone-explain-the-debounce-function-in-javascript) – Shilly May 17 '19 at 08:08

3 Answers3

1
var scrollstop = false;

$(window).on('mousewheel', function(event) {
  if(!scrollstop) {
    scrollstop = true;
    setTimeout(() => { scrollstop = false; }, 1000);
  }
}

Something like this?

user8689373
  • 115
  • 1
  • 11
  • @N15M0_jk yeah, your one looks a bit better with the boolean initially set to true, it feels a little more intuitive. – user8689373 May 17 '19 at 08:40
1

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);
jarodsmk
  • 1,876
  • 2
  • 21
  • 40
0
var pauseWheel = false;
$(window).on('mousewheel', function(event) {
     if(pauseWheel) {
         event.preventDefault();
     }
     else {
         pauseWheel = true;
         setTimeout(function(){ pauseWheel = false; }, 1000);
     }
}

pauseWheel is a variable true if the mousewheel event fired in the last 1000ms.

heraphim
  • 326
  • 2
  • 10