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
54
votes
6 answers

clearInterval() not working

Possible Duplicate: JS - How to clear interval after using setInterval() I have a function that changes the font-family of some text every 500 ms using setInterval (I made it just to practice JavaScript.) The function is called by clicking on an…
Matt Sanchez
  • 583
  • 1
  • 6
  • 10
47
votes
9 answers

How can I clearInterval() for all setInterval()?

I've got a setInterval() called in a jQuery plugin, but I want to clear it from my main page, where I don't have access to the variable that the setInterval was stored in. Is there a way to clear all timers present on a page?
Matt
46
votes
1 answer

Jquery/Ajax call with timer

I have a php page that echos out rows from a database. I want to call it via jquery/ajax every 30 seconds. But I also want to be able to call the page at any time so that if I add a record via the form, once the form submits I want the page via…
John
  • 9,840
  • 26
  • 91
  • 137
43
votes
6 answers

setInterval/setTimeout return value

Two questions: How is the value returned from setInterval and setTimeout (the ones used to clear the timers) calculated? Is it possible for both the functions to return the same value during runtime? For example: var a = setInterval(fn1, 1000); var…
aditya
  • 1,978
  • 3
  • 17
  • 22
43
votes
3 answers

Checking whether clearInterval has been called?

Given this code: bob = setInterval(function, 1000); clearInterval(bob); Is there now a way to know if that interval has been cleared? Currently, I keep track of this myself, by unsetting 'bob', but I'm curious if my extra line of code is…
swajak
  • 2,669
  • 3
  • 20
  • 22
37
votes
5 answers

Javascript setInterval not working

I need to run a javascript function each 10 seconds. I understand the syntax must work like follow but I am not getting any success: function funcName() { alert("test"); } var func = funcName(); var run = setInterval("func",10000) But this…
mauzilla
  • 3,574
  • 10
  • 50
  • 86
37
votes
2 answers

Pausing setInterval when page/ browser is out of focus

I have a simple slideshow that I've made on a client's homepage, using setInterval to time the rotations. To prevent browsers from screwing up setInterval when the page isn't in focus (another tab is being viewed, or another programme), I'm…
Michael Watson
  • 1,079
  • 3
  • 14
  • 26
36
votes
7 answers

clearInterval in React

I'm new at React and I was trying to create a simple stopwatch with a start and stop buttons. I'm banging my head against the wall to try to clearInterval with an onClick event on Stop button. I would declare a variable for the setInterval and then…
Lucas
  • 361
  • 1
  • 3
  • 3
35
votes
6 answers

setInterval with loop time

setInterval(function(){}, 200) this code run the function each 200 miliseconds, how do I do it if I only want the function to be ran 10 times. thanks for help.
bingjie2680
  • 7,643
  • 8
  • 45
  • 72
34
votes
6 answers

setInterval timing slowly drifts away from staying accurate

It seems that when I setInterval for 1000ms, it actually fires the function every 1001ms or so. This results in a slow temporal drift the longer its running. var start; var f = function() { if (!start) start = new Date().getTime(); var diff…
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
34
votes
7 answers

How to use setInterval function within for loop

I'm trying to run multiple timers given a variable list of items. The code looks something like this: var list = Array(...); for(var x in list){ setInterval(function(){ list[x] += 10; console.log(x + "=>" + list[x] + "\n"); …
hyleaus
  • 755
  • 1
  • 8
  • 21
33
votes
1 answer

Detecting class change without setInterval

I have a div that has additional classes added to it programmatically. How can I detect the class name change without using this setInterval implementation? setInterval(function() { var elem = document.getElementsByClassName('original')[0]; …
David
  • 13,619
  • 15
  • 45
  • 51
31
votes
8 answers

How to start and stop/pause setInterval?

I'm trying to pause and then play a setInterval loop. After I have stopped the loop, the "start" button in my attempt doesn't seem to work : input = document.getElementById("input"); function start() { add = setInterval("input.value++",…
Web_Designer
  • 72,308
  • 93
  • 206
  • 262
30
votes
2 answers

Node.js crashes when using long interval in setinterval

function createSasTokenTimer() { console.log("Hello"); } setInterval(createSasTokenTimer, 3000000); I run this code and after 50 minutes I get the following error: Hello timers.js:265 callback.apply(this, args); …
janiv
  • 749
  • 2
  • 6
  • 16
30
votes
2 answers

Call JavaScript function after 1 second One Time

I have successfully managed to make a div hide on click after 400 milliseconds using a setInterval function. My issue is that it runs continually, I only need the function to execute once. After a quick search I discovered that the setInterval can…
BLK Horizon
  • 391
  • 1
  • 3
  • 7