1

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?

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

Comments

1

Keep a counter:

var timesCalled = 0;

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

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

(clearInterval)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.