Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery scroll with timeout

I set up timeout with jquery at scroll action. For example, after scroll wait 10 seconds and send ajax request, but how to cancel previous timeout if receive new action of scroll withing first timeout not processed?

like image 623
abrahab Avatar asked May 13 '12 15:05

abrahab


1 Answers

Use clearTimeout:

var timer;

$(window).scroll(function(){

    if ( timer ) clearTimeout(timer);

    timer = setTimeout(function(){
        // Make your AJAX request here...
    }, 10000);
});
like image 72
Joseph Silber Avatar answered Nov 03 '22 15:11

Joseph Silber