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
102
votes
23 answers

Python Equivalent of setInterval()?

Does Python have a function similar to JavaScript's setInterval()? I would like to have: def set_interval(func, interval): ... That will call func every interval time units.
zjm1126
  • 63,397
  • 81
  • 173
  • 221
101
votes
13 answers

How to make `setInterval` behave more in sync, or how to use `setTimeout` instead?

I am working on a music program that requires multiple JavaScript elements to be in sync with another. I’ve been using setInterval, which works really well initially. However, over time the elements gradually become out of sync which is bad in a…
user3084366
  • 1,103
  • 2
  • 9
  • 12
93
votes
3 answers

How to exit from setInterval

I need to exit from a running interval if the conditions are correct: var refreshId = setInterval(function() { var properID = CheckReload(); if (properID > 0) { <--- exit from the loop---> } }, 10000);
Martin Ongtangco
  • 22,657
  • 16
  • 58
  • 84
92
votes
6 answers

Calculating Page Load Time In JavaScript

I am trying to make a webpage that, when it starts loading, uses an Interval to start a timer. When the page fully loads, it stops the timer, but 99% of the time i get time measurements of 0.00 or 0.01 even if it takes longer. Occasionally, it says…
austinstout
  • 1,180
  • 1
  • 11
  • 14
91
votes
5 answers

Javascript setInterval function to clear itself?

myInterval = setInterval(function(){ MyFunction(); },50); function MyFunction() { //Can I call clearInterval(myInterval); in here? } The interval's not stopping (not being cleared), if what I've coded above is fine then it'll help me look…
user1017882
90
votes
10 answers

How can I pause setInterval() functions?

How do I pause and resume the setInterval() function using Javascript? For example, maybe I have a stopwatch to tell you the number of seconds that you have been looking at the webpage. There is a 'Pause' and 'Resume' button. The reason why…
chris97ong
  • 6,870
  • 7
  • 32
  • 52
89
votes
4 answers

How to start setInterval loop immediately?

In a simple setInterval setInterval(function() { // Do something every 9 seconds }, 9000); The first action will happen after 9 seconds (t=9s). How to force the loop to perform the first action immediately (t=0)? I think it is due to the…
Googlebot
  • 15,159
  • 44
  • 133
  • 229
85
votes
6 answers

Is setInterval CPU intensive?

I read somewhere that setInterval is CPU intensive. I created a script that uses setInterval and monitored the CPU usage but didn't notice a change. I want to know if there is something I missed. What the code does is check for changes to the hash…
Ryuku
  • 853
  • 1
  • 6
  • 5
80
votes
9 answers

JavaScript setInterval and `this` solution

I need to access this from my setInterval handler prefs: null, startup : function() { // init prefs ... this.retrieve_rate(); this.intervalID = setInterval(this.retrieve_rate, this.INTERVAL); }, retrieve_rate…
Pablo
  • 28,133
  • 34
  • 125
  • 215
80
votes
2 answers

Why does the setInterval callback execute only once?

I have this counter I made but I want it to run forever, it's really simple, what am I doing wrong here? function timer() { console.log("timer!") } window.setInterval(timer(), 1000)
computer_smile
  • 2,117
  • 2
  • 24
  • 42
74
votes
5 answers

Using setInterval() to do simplistic continuous polling

For a simple web app that needs to refresh parts of data presented to the user in set intervals, are there any downsides to just using setInterval() to get a JSON from an endpoint instead of using a proper polling framework? For the sake of an…
Sologoub
  • 5,312
  • 6
  • 37
  • 65
72
votes
9 answers

Does JavaScript setInterval() method cause memory leak?

Currently developing a JavaScript based animation project. I have noticed that, proper use of setInterval(), setTimeout() and even requestAnimationFrame allocates memory without my request, and causes frequent garbage collection calls. More GC calls…
matahari
  • 737
  • 1
  • 5
  • 6
62
votes
9 answers

What is the equivalent of javascript setTimeout in Java?

I need to implement a function to run after 60 seconds of clicking a button. Please help, I used the Timer class, but I think that that is not the best way.
55
votes
3 answers

Will setInterval drift?

This is a pretty simple question really. If I use setInterval(something, 1000), can I be completely sure that after, say, 31 days it will have triggered "something" exactly 60*60*24*31 times? Or is there any risk for so called drifting?
Deniz Dogan
  • 25,711
  • 35
  • 110
  • 162
55
votes
4 answers

In AngularJS, how to detect when user leaves template/page?

I am using the Javascript command: setInterval. I like to stop it when the user leaves the page. This code seems to work well: http://jsfiddle.net/PQz5k/ It detects when a user leaves a page. It executes Javascript code when a user clicks on a…
Curt
  • 1,181
  • 3
  • 19
  • 29