Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
124
rated 0 times [  129] [ 5]  / answers: 1 / hits: 40213  / 15 Years ago, fri, october 9, 2009, 12:00:00

How can I repeat a function doSomething() every 5 seconds.



I also need code that will make it stop doing it.



And code to on-the-fly adjust the frequency.


More From » jquery

 Answers
15

setTimeout() will only launch the command once. In this case, setInterval() is your friend.



var iFrequency = 5000; // expressed in miliseconds
var myInterval = 0;

// STARTS and Resets the loop if any
function startLoop() {
if(myInterval > 0) clearInterval(myInterval); // stop
myInterval = setInterval( doSomething(), iFrequency ); // run
}

function doSomething()
{
// (do something here)
}


from code...



<input type=button onclick=iFrequency+=1000; startLoop(); return false; 
value=Add 1 second more to the interval />

[#98543] Monday, October 5, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaceyr

Total Points: 510
Total Questions: 97
Total Answers: 116

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
kaceyr questions
;