Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeating setTimeout

I am trying to repeat setTimeout every 10 seconds. I know that setTimeout by default only waits and then performs an action one time. How can I repeat the process?

setTimeout(function() {   setTimeout(function() {     console.log("10 seconds");   }, 10000); }, 10000); 
like image 551
Daniel Avatar asked Jul 24 '12 04:07

Daniel


People also ask

How do I make setTimeout repeat?

To repeat a function indefinitely, setTimeout can be called recursively: function repeatingFunc() { console. log("It's been 5 seconds. Execute the function again."); setTimeout(repeatingFunc, 5000); } setTimeout(repeatingFunc, 5000);

Does setTimeout run multiple times?

setTimeout allows us to run a function once after the interval of time. setInterval allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval.

How many times does setTimeout run?

2 Answers. Show activity on this post. setTimeout will only execute once.

How do you stop a setTimeout loop?

To stop a setTimeout loop with JavaScript, we can call the clearTimeout function. let timeOutVar; const myFunc = (terminator = false) => { if (terminator) { clearTimeout(timeOutVar); } else { timeOutVar = setTimeout(myFunc, 1000); } }; myFunc(true); myFunc(false);


1 Answers

Maybe you should use setInterval()

like image 119
uzyn Avatar answered Oct 01 '22 13:10

uzyn