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
1745
votes
9 answers

Stop setInterval call in JavaScript

I am using setInterval(fname, 10000); to call a function every 10 seconds in JavaScript. Is it possible to stop calling it on some event? I want the user to be able to stop the repeated refresh of data.
cnu
  • 36,135
  • 23
  • 65
  • 63
808
votes
20 answers

setTimeout or setInterval?

As far as I can tell, these two pieces of javascript behave the same way: Option A: function myTimeoutFunction() { doStuff(); setTimeout(myTimeoutFunction, 1000); } myTimeoutFunction(); Option B: function myTimeoutFunction() { …
Damovisa
  • 19,213
  • 14
  • 66
  • 88
535
votes
18 answers

Execute the setInterval function without delay the first time

It's there a way to configure the setInterval method of javascript to execute the method immediately and then executes with the timer
Jorge
  • 17,896
  • 19
  • 80
  • 126
373
votes
19 answers

Pass parameters in setInterval function

Please advise how to pass parameters into a function called using setInterval. My example setInterval(funca(10,3), 500); is incorrect.
Rakesh
  • 5,793
  • 8
  • 36
  • 37
304
votes
15 answers

Calling a function every 60 seconds

Using setTimeout() it is possible to launch a function at a specified time: setTimeout(function, 60000); But what if I would like to launch the function multiple times? Every time a time interval passes, I would like to execute the function (every…
Richard Knop
  • 81,041
  • 149
  • 392
  • 552
262
votes
17 answers

How can I make setInterval also work when a tab is inactive in Chrome?

I have a setInterval running a piece of code 30 times a second. This works great, however when I select another tab (so that the tab with my code becomes inactive), the setInterval is set to an idle state for some reason. I made this simplified test…
pimvdb
  • 151,816
  • 78
  • 307
  • 352
216
votes
1 answer

Can clearInterval() be called inside setInterval()?

bigloop=setInterval(function () { var checked = $('#status_table tr [id^="monitor_"]:checked'); if (checked.index()===-1 ||checked.length===0 || ){ bigloop=clearInterval(bigloop); …
yvonnezoe
  • 7,129
  • 8
  • 30
  • 47
196
votes
17 answers

Changing the interval of SetInterval while it's running

I have written a javascript function that uses setInterval to manipulate a string every tenth of a second for a certain number of iterations. function timer() { var section = document.getElementById('txt').value; var len = section.length; …
Joe Di Stefano
  • 2,157
  • 2
  • 13
  • 8
160
votes
16 answers

How to create an accurate timer in javascript?

I need to create a simple but accurate timer. This is my code: var seconds = 0; setInterval(function() { timer.innerHTML = seconds++; }, 1000); After exactly 3600 seconds, it prints about 3500 seconds. Why is it not accurate? How can I create an…
xRobot
  • 25,579
  • 69
  • 184
  • 304
153
votes
13 answers

Code for a simple JavaScript countdown timer?

I want to use a simple countdown timer starting at 30 seconds from when the function is run and ending at 0. No milliseconds. How can it be coded?
Michael Swarts
  • 3,813
  • 8
  • 31
  • 41
136
votes
6 answers

Stop setInterval

I want to stop this interval in the error handler from running repeatedly. Is that possible, and if so, how? // example code $(document).on('ready',function(){ setInterval(updateDiv,3000); }); function updateDiv(){ $.ajax({ url:…
Henrik Petterson
  • 6,862
  • 20
  • 71
  • 155
130
votes
8 answers

Do something every x minutes in Swift

How can I run a function every minute? In JavaScript I can do something like setInterval, does something similar exist in Swift? Wanted output: Hello World once a minute...
JOSEFtw
  • 9,781
  • 9
  • 49
  • 67
117
votes
4 answers

How to stop "setInterval"

How do I stop and start setInterval? Suppose I have a textarea. I want to stop setInterval on focus and restart setInterval on blur (with jQuery).
testkhan
  • 1,769
  • 4
  • 14
  • 12
115
votes
7 answers

Viewing all the timeouts/intervals in javascript?

I'm writing an application that utilizes JavaScript timeouts and intervals to update the page. Is there a way to see how many intervals are setup? I want to make sure that I'm not accidentally going to kill the browser by having hundreds of…
Omar Kooheji
  • 54,530
  • 68
  • 182
  • 238
115
votes
3 answers

How do I reset the setInterval timer?

How do I reset a setInterval timer back to 0? var myTimer = setInterval(function() { console.log('idle'); }, 4000); I tried clearInterval(myTimer) but that completely stops the interval. I want it to restart from 0.
Rik de Vos
  • 3,467
  • 5
  • 28
  • 34
1
2 3
99 100