最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript setInterval Limits? - Stack Overflow

programmeradmin2浏览0评论

I have an application using JavaScript's setInterval() to run a digital clock. I was wondering if it has a timeout, or limit, to the amount of times it can execute this function.

I have an application using JavaScript's setInterval() to run a digital clock. I was wondering if it has a timeout, or limit, to the amount of times it can execute this function.

Share Improve this question asked Oct 20, 2011 at 14:54 Ethan TurkeltaubEthan Turkeltaub 2,9618 gold badges33 silver badges45 bronze badges 1
  • You mean how many times the callback will be executed? No, there is no limit. Why was this question downvoted? – davin Commented Oct 20, 2011 at 14:58
Add a comment  | 

3 Answers 3

Reset to default 37

setInterval() will run infinitely.

If you wish to terminate the 'loop' you can use clearInterval. For example:

var counter = 0;

var looper = setInterval(function(){ 
    counter++;
    console.log("Counter is: " + counter);

    if (counter >= 5)
    {
        clearInterval(looper);
    }

}, 1000);

No, the given function will keep being executed until you clear the interval manually with clearInterval()

Note that in most browsers, your function will still be executed when the page is in a background tab, but mobile browsers (notably IOS5 Safari) may free the page until its focused / visible again.

As others have mentioned, there is not limit to the number of times your interval will run, however if you intend to run a timer indefinitely you should consider the info here:

Minimum setInterval()/setTimeout() delay on background tabs

If your user is likely to tab-out, 1 second seems to be safe minimum interval

发布评论

评论列表(0)

  1. 暂无评论