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

Wait until setInterval() is done

I would like to add a small dice-rolling effect to my Javascript code. I think a good way is to use the setInterval() method. My idea was the following code (just for testing): function roleDice() { var i = Math.floor((Math.random() * 25) + 5); …
Andre Hofmeister
  • 3,185
  • 11
  • 51
  • 74
29
votes
5 answers

setInterval() behaviour with 0 milliseconds in JavaScript

In my application I found some JavaScript code that is using setInterval with 0 milliseconds, like so: self.setInterval("myFunction()",0); Obviously, this does not seem like a good idea to me. Can anyone tell me what will be the behaviour of…
João Gomes
  • 371
  • 1
  • 4
  • 6
29
votes
5 answers

How to clearInterval with unknown ID?

Say someone (evil) has set us a timer with setInterval, but we don't know its ID (we don't have the reference to the object, that setInterval is returning, nor its value) (function(){ setInterval(function(){console.log('pwned')}, …
mykhal
  • 19,175
  • 11
  • 72
  • 80
28
votes
3 answers

How to setInterval for every 5 second render with React hook useEffect in React Native app?

I have React Native app and I get data from API by fetch. I created custom hook that get data from API. And i need to re-render it every 5 seconds. For it I wrapped my custom hook to setInterval and after my app become work very slowly and when I…
jocoders
  • 1,594
  • 2
  • 19
  • 54
28
votes
17 answers

Making a live clock in javascript

The clock kinda works. But instead of replacing the current time of day it prints a new time of day every second. I understand why it does that but I don't know how to fix it. I would appreciate if you could give me some tips without saying the…
WilliamG
  • 451
  • 1
  • 8
  • 16
28
votes
2 answers

Check if an Interval is running and viceversa

So i have a web app with basic authentication. When im logged in, an Interval is set: $("#login").click(function(e) { var interval = setInterval(function(){myFunction();}, 2000); }); Then when im logged out i need to stop the…
xnanitox
  • 475
  • 1
  • 6
  • 9
27
votes
4 answers

Angular2/4 : Refresh Data Realtime

I need to refresh the data in a component page in an interval. Also I need to refresh the data after doing some action. I am using Obeservables in the service so that I can subscribe to when the response is ready. I am pushing the subscriptions to a…
26
votes
3 answers

Angular 2 call setInterval() undefined Services form Dependency injection

I want to call a function every 10 minutes by using setInterval() and in this function I want to use a Service (called auth) that I get from the Dependency Injector of Angular 2, the problem is that the console tells me following: EXCEPTION:…
Alex
  • 372
  • 1
  • 4
  • 11
25
votes
6 answers

What is the maximum delay for setInterval?

I'm having problems on Firefox 15 and Chrome 21 with the following code: setInterval(function () { console.log('test') }, 300000000000) On both browsers, the function is run right away repeats very quickly. Sure, that's a big number (representing…
Nogwater
  • 2,777
  • 3
  • 28
  • 28
22
votes
1 answer

Setting a timer in Node.js

I need to run code in Node.js every 24 hours. I came across a function called setTimeout. Below is my code snippet var et = require('elementtree'); var XML = et.XML; var ElementTree = et.ElementTree; var element = et.Element; var subElement =…
Amanda G
  • 1,931
  • 10
  • 33
  • 43
21
votes
3 answers

JavaScript setInterval Limits?

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.
Ethan Turkeltaub
  • 2,931
  • 8
  • 30
  • 45
21
votes
4 answers

Equivalent of setInterval in python

I have recently posted a question about how to postpone execution of a function in Python (kind of equivalent to Javascript setTimeout) and it turns out to be a simple task using threading.Timer (well, simple as long as the function does not share…
Andrea
  • 20,253
  • 23
  • 114
  • 183
20
votes
6 answers

Allow running setInterval more than once per millisecond in nodejs

I have a node script which is supposed to utilize all the CPU resources a single node process could get. But I found setInterval to be too slow. And sure enough I found this in the documentation: When delay is larger than 2147483647 or less than 1,…
Forivin
  • 14,780
  • 27
  • 106
  • 199
20
votes
3 answers

setTimeout or setInterval or requestAnimationFrame

For HTML5 games,with canvas animation for mobile devices. I'm facing some performance issues which differ the speed between each device and the others. requestAnimationFrame speed the animation of the game according to the device speed. setInterval…
Soliman
  • 1,132
  • 3
  • 12
  • 32
20
votes
17 answers

JavaScript: How to do something every full hour?

I want to execute some JS code every hour. But I can't use setInterval("javascript function",60*60*1000); because I want to do it every full hour, I mean in 1:00, in 2:00, in 3:00 and so on. I am thinking about something like var d; while(true) { …
matlos
  • 401
  • 1
  • 6
  • 11