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.
setInterval() simply put, is a timed loop.
The setInterval() method calls a function at specified intervals (in milliseconds). The setInterval() method continues calling the function until clearInterval() is called, or the window is closed. 1 second = 1000 milliseconds.
Answer: Use the clearInterval() Method The setInterval() method returns an interval ID which uniquely identifies the interval. You can pass this interval ID to the global clearInterval() method to cancel or stop setInterval() call.
Yes, setInterval repeats until you call clearInterval with the interval to stop.
Use a counter which increments each time the callback gets executed, and when it reaches your desired number of executions, use clearInterval()
to kill the timer:
var counter = 0; var i = setInterval(function(){ // do your thing counter++; if(counter === 10) { clearInterval(i); } }, 200);
(function(){ var i = 10; (function k(){ // your code here if( --i ) { setTimeout( k, 200 ); } })() })()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With