abrahab
abrahab

Reputation: 2500

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?

Upvotes: 5

Views: 14593

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 219920

Use clearTimeout:

var timer;

$(window).scroll(function(){

    if ( timer ) clearTimeout(timer);

    timer = setTimeout(function(){
        // Make your AJAX request here...
    }, 10000);
});

Upvotes: 15

Related Questions