0

Any idea how to make on scroll event function to ignore the on scroll event in javascript/jquery.

for example : I want something like this :

$(document).scroll(function(){
  disable on scroll of mouse (such that $(document).scroll(function() should  not be called when user scroll the page)

 // do stuff

enable on scroll of mouse (such that $(document).scroll(function() can be called when the user scrolls the page)
        });

Any help will be highly useful
Deva
  • 91
  • 6
  • This is not very clear, you can't really prevent the user from scrolling, and if you don't want to call the event handler, just don't add an event handler – adeneo Feb 06 '16 at 16:24
  • Please check my question again – Deva Feb 06 '16 at 16:25
  • You want to disable it on specific use case or what? You should provide minimalistic sample replicating your issue/expected behaviour because quite unclear waht you are asking imho – A. Wolff Feb 06 '16 at 16:30
  • @A.Wolff : Yes. Actually the function is getting called multiple times when user scrolls down the page. – Deva Feb 06 '16 at 16:36
  • 1
    So what you really want is to throttle or debounce the event. Search for something like "debounce scroll event", and you'll find a multitude of answers – adeneo Feb 06 '16 at 16:46
  • **↑↑↑** e.g http://stackoverflow.com/questions/9144560/jquery-scroll-detect-when-user-stops-scrolling – A. Wolff Feb 06 '16 at 16:46
  • @A.Wolff : debounce/timeout is not a permament solution. it will only delay the call..what if the user scolls just after the timeout period while the previous call still in execution – Deva Feb 06 '16 at 16:51
  • Why don't you clearly explain your issue/expected behaviour??? – A. Wolff Feb 06 '16 at 16:54
  • @A.Wolff :I am trying to implement a facebook type auto load script where the content should auto load when the user scrolls down the page. Here when the user scrolls down the page then the $(document).scroll(function() will be called but before it can complete with the ajax call inside the function, It is getting called again ...due to which there are duplicate content appearing in my page...so I want the solution like whenever the function is called it should ignore the on scroll event and after finishing with its ajax call it should enable it again. – Deva Feb 06 '16 at 17:01
  • So basically you want to abort any previously called ajax request. See [aborting ajax request](http://stackoverflow.com/questions/446594/abort-ajax-requests-using-jquery) and btw read the [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Or you coud set a flag waiting for any previously request done (completes) before sending any new one. – A. Wolff Feb 06 '16 at 17:05

0 Answers0