Ask coding questions

← Back to all posts
Do somthing every second
LukeShomper (26)

How would I make a script do an action every second?

Answered by mwilki7 (741) [earned 5 cycles]
View Answer
Commentshotnewtop
mwilki7 (741)
setInterval(function(){ 
    // your code here to be done every second
}, 1000); // 1000 milliseconds = 1 second

More examples here
https://www.w3schools.com/jsref/met_win_setinterval.asp

If you want to stop it do:

var my_interval = setInterval(function() {
     // code here
}, 1000);


clearInterval(my_interval);

setTimeout is basically the same thing if you want a setInterval that only does it once.