Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
90
rated 0 times [  92] [ 2]  / answers: 1 / hits: 101303  / 13 Years ago, wed, december 7, 2011, 12:00:00
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.


More From » setinterval

 Answers
10

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);

[#88688] Tuesday, December 6, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jennie

Total Points: 593
Total Questions: 102
Total Answers: 106

Location: Federated States of Micronesia
Member since Fri, Sep 16, 2022
2 Years ago
jennie questions
;