Sajan Parikh
Sajan Parikh

Reputation: 4950

jQuery - End setInterval Loop

I'm running the following.

setInterval(function()
{
    update(url, baseName(data));
}
, 1000);

This calls that update function every second.

Is there a way to keep this functionality of calling update every second, but killing it or ending it after 10 seconds?

Upvotes: 1

Views: 5993

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388406

Have a counter and store the interval reference, then use clearInterval() to end the calls

var counter = 0;
var timer = setInterval(function () {
    counter++;
    update(url, baseName(data));
    if(counter>=10){
        clearInterval(timer)
    }
}, 1000);

Upvotes: 2

rninty
rninty

Reputation: 1082

Keep a counter:

var timesCalled = 0;

var t = setInterval(function() {
    update(url, baseName(data));
    timesCalled++;

    if (timesCalled === 10)
        clearInterval(t);
}, 1000);

(clearInterval)

Upvotes: 1

Related Questions