views:

263

answers:

6

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.

A: 

You could use setTimeout() for this.

http://www.w3schools.com/htmldom/met%5Fwin%5Fsettimeout.asp

svens
how do I stop it? And how do I adjust the frequency on-the-fly?
steven
+3  A: 

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" />
pixeline
How do I adjust the frequency on-the-fly? And what code will stop setInterval()?
steven
Ok clearInterval stops it, but what about the frequency?
steven
the frequency is the second argument of the function. See the link to the tutorial.
pixeline
but how can I change the freq when the interval is in progress?
steven
edited so you can see how to change the frequency
balexandre
to modify the frequency on the fly, you would have to clear the interval and recreate it with the new frequency each time. Wrap all this in a helper function that deals with the setInterval/clearInterval control, function to which you pass the frequency as argument, and, why not, the function to execute.
pixeline
eg.0: starts..1: timerchanged to 1 sec. <-- should exec here.2: Doit();
steven
balexandre code edits make it happen. Try again, steven.
pixeline
done editing ... one method to call only, if there's any loop it will stop first and apply new frequency ... if first time, it only starts it. Much Simpler this way
balexandre
There is no =+ operator... Perhaps you meant to use the += operator?
Guffa
corrected, thanks.
pixeline
+2  A: 

Try jQuery Timers

Guillem Gelabert
The newest version is available at the jQuery Plugins site.http://plugins.jquery.com/project/timers
Blair Mitchelmore
+2  A: 

Use

setInterval

Calls a function repeatedly, with a fixed time delay between each call to that function.

for repeated action and

clearInterval

Cancels repeated action which was set up using setInterval().

to stop that

rahul
Ok clearInterval stops it, but what about changing the frequency?
steven
You can change the delay in the setInterval
rahul
but how can I change the freq when the interval is in progress?
steven
A: 
<script type="text/javascript">
var t; var timer_is_on=0; var timeout=5000;

function timedCount() {
  doSomeThing();
  t = setTimeout("timedCount()",timeout);
}

function doTimer() {
  if (!timer_is_on) {
    timer_is_on=1;
    timedCount();
  }
}

function stopCount() {
  clearTimeout(t);
  timer_is_on=0;
}
function changeFreq() {
   timeout = 2000;
}
</script>
jitter
nice. Close, doesn't change the frequency on the fly but I'm not sure if that's possible now.
steven
A: 
var myInterval = setInterval ( doSomething, 5000 );

function doSomething ( )
{
  alert("doing something")
}

And to remove it:

clearInterval(myInterval);
jmav
-1 some thing, same code already in... read first please, no need duplicate questions.
balexandre
I read it, but i fix syntax.And where is code below now?
jmav
you should know how to look? http://stackoverflow.com/revisions/1542294/list
balexandre
Thx, I'm new at this, and i'm trying my best.
jmav