Questions tagged [setinterval]

setInterval is a global JavaScript method. It is used to execute a particular function or piece of code at regular intervals.

Tag usage

The tag is appropriate for questions involving the setInterval method.

Implementation

The primary implementation of setInterval receives as arguments a JavaScript function and a number indicating the number of milliseconds in the interval. Ideally, the function would run at the given interval, but this is not always the case if the execution of the function takes longer than the interval. In such a case, MDN recommends using setTimeout instead.

The first parameter to setInterval may also be a code string instead of an actual function, but this usage is generally not recommended for the same reasons that using eval() is frowned upon.

References

4714 questions
19
votes
2 answers

Listeners in Chrome dev tools' performance profiling results

I have been profiling a React app using Chrome dev tools and I've discovered a linearly increasing Listener count. Take a look at the screenshot below. The Listeners are in orange. I narrowed it down to a simple countdown value render inside p…
19
votes
2 answers

Problem with IE and setInterval() not refreshing/updating

I'm using JavaScript/Jquery to make a page auto-update with a value from a database, although it doesn't seem to update in Internet Explorer. It works fine in FireFox & Chrome. Can anyone explain what's wrong? It looks like IE is just displaying a…
Joey Morani
  • 25,431
  • 32
  • 84
  • 131
18
votes
5 answers

jquery function setInterval

$(document).ready(function(){ setInterval(swapImages(),1000); function swapImages(){ var active = $('.active'); var next = ($('.active').next().length > 0) ? $('.active').next() : $('#siteNewsHead img:first'); …
callum.bennett
  • 678
  • 3
  • 12
  • 28
18
votes
5 answers

Does the browser keep track of active timer IDs?

Does the browser keep track of active setInterval and setTimeout IDs? Or is this solely up to the developer to keep track of? If it does keep track of them, is it accessible via the BOM?
ground5hark
  • 4,464
  • 8
  • 30
  • 35
18
votes
1 answer

setInterval and long running functions

How does setInterval handle callback functions that take longer than the desired interval? I've read that the callback may receive the number of milliseconds late as its first argument, but I was unable to find why it would be late (jitter, or long…
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
16
votes
3 answers

setInterval not working (firing only once) in Google Chrome extension

Just as the title says: setInterval is only firing its callback once. manifest.json: { //... "content_scripts" : [{ "js" : ["code.js"], //... }], //... } code.js (example): setInterval(alert('only shown…
16
votes
4 answers

jQuery - How to restart setInterval after killing it off with clearInterval?

I want to create a page with 2 buttons, 'STAY' and 'Leave'. There is an iFrame underneath the buttons. When the page loads for the first time, the iFrame starts refreshing automatically after 10 secs. When the user hits STAY button, it will stop…
Bilbo Baggins
  • 3,644
  • 8
  • 40
  • 64
16
votes
8 answers

setInterval function without arrow function

I am learning about react components following the documentation https://facebook.github.io/react/docs/state-and-lifecycle.html Why do we need to use arrow function here: this.timerID = setInterval(() => this.tick(), 1000); Why can't I just say…
gvaswani
  • 161
  • 1
  • 1
  • 3
16
votes
1 answer

Is there a maximum delay limit for window.setInterval

Today I encountered an interesting problem with window.setInterval. When used with a sufficiently large delay (in this case the number of milliseconds in 30 days) it executes every second instead of every 30 days. Tested in latest Chrome and…
tomfumb
  • 3,669
  • 3
  • 34
  • 50
16
votes
3 answers

How to execute code exactly 1000 times in 1 second in JavaScript

n = 0; var timer = setInterval(function() { if (n == 0) { console.log(new Date()); } // execute some other code here n++; if (n == 1000) { clearInterval(timer); console.log(new Date()); …
Jswq
  • 758
  • 1
  • 7
  • 23
16
votes
3 answers

Javascript: Run setInterval only once

I would like to run the following code only once, so after 2 seconds it will change the iframe's src, but won't try to do it again and again.