Antonis
Antonis

Reputation: 361

Stop timers on user scroll

I have a div tag that fills with data when an Ajax call is made to my server. The Ajax request is fired every 10 seconds from within a timer with setInterval. This div has a scroll bar.

I would like to disable Ajax within timer whenever user scrolls.

Upvotes: 1

Views: 403

Answers (2)

Noa
Noa

Reputation: 1216

try like this:

var t = setInterval(function(){ 
            /*ajax call here*/ 
        }, 10000);

divId.addEventListener("scroll", function(){
    clearInterval(t);
});

Upvotes: 2

Aju John
Aju John

Reputation: 2234

Use clearInterval function to clear the setInterval action once user scrolls. This function will basically clears a timer set with the setInterval() method.

var sInterval = setInterval(function xyz () {}, 10*1000);

//And in the scroll event just clear the interval,
clearInterval(sInterval);

Upvotes: 2

Related Questions