oshirowanen
oshirowanen

Reputation: 15925

Help chosing between settimeout and setinterval

UPDATE 2:

OK, looks like it runs the first time after a minute. How do I get it to run onload, then every minute after that?

UPDATE 1:

I've tried: var interval = setInterval(get_reported_incidents, 60000); but nothing happens. in get_reported_incidents(); I just have an alert("hello");.

ORIGINAL QUESTION:

I want to run a function every minute:

get_reported_incidents();

But I am not sure which method is best for this task;

settimeout or setinterval

Upvotes: 1

Views: 2745

Answers (7)

venimus
venimus

Reputation: 6057

obviously setinterval might be easier to maintain in your case

get_reported_incidents(); //first call
var interval = setInterval(get_reported_incidents, 60000);

vs

var interval;
function timeout (){
    get_reported_incidents();
    interval=setTimeout(timeout, 60000);
}
timeout();

Upvotes: 0

Alnitak
Alnitak

Reputation: 339917

Strictly speaking, setInterval() was designed for repeating events and setTimeout() for one-shot events.

However you will tend to find that with setTimeout() time will "creep" gradually. I've not tried this at 1 minute intervals, but with a 1 second timer I found it happened quite a lot. A clock showing the current time (to the nearest millisecond) would show a steady increase in the millisecond value of "now".

See http://jsfiddle.net/alnitak/LJCJU/ and tweak the interval to see what I mean!

So, for greatest accuracy, I do this:

var timerHandler = function() {
    var interval = 60000;
    // do some stuff
    ...

    var now = new Date();
    var delay = interval - (now % interval);
    setTimeout(timerHandler, delay); 
};

This is ideal if you want the timer events to be started in sync with the clock on your system, rather than at some unspecified time "roughly every minute".

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074949

It's totally personal preference. setInterval has some odd edge cases around what happens when the previous interval's code hasn't finished running before the next interval is due to start (not a problem if your interval is every minute; intervals every second, which one sometimes wants, get a bit tricky).

I tend to prefer chained setTimeout calls (where each schedules the next) because they can't run away with you — your code always has to explicitly say "Okay, and call me back again next time." But properly-written code should work with either.

Chained setTimeout calls are also more well-suited to asynchronous operations, like for instance polling something via ajax, because you don't schedule the next timeout until the ajax operation completes. Using setInterval, because the ajax calls are asynchronous, you could end up overlapping them.

Upvotes: 3

Josiah Ruddell
Josiah Ruddell

Reputation: 29831

use setTimeout recursively. See here for more information on why setInterval is a poor choice.

function timeout (){
    get_reported_incidents();
    setTimeout(timeout, 1000 * 60);
}

timeout(); // start

Upvotes: 1

George Cummins
George Cummins

Reputation: 28906

setinterval executes a function at a given interval. settimeout executes a function after a specified wait time, and then exits.

If you are attempting to do a cron-like execution every minute, you will want to use setinterval.

Please see http://javascript.about.com/library/blstvsi.htm for a comparison.

Upvotes: 1

gen_Eric
gen_Eric

Reputation: 227280

setTimeout runs a command once after a period of time. setInterval runs a command every time interval.

So, to run get_reported_incidents every minute use setInterval.

var interval = setInterval(get_reported_incidents, 60000);

Upvotes: 1

Guffa
Guffa

Reputation: 700552

Using setinterval would be the more natural choise. If you use setTimeout, you have to start a new timeout from the event handler.

window.setInterval(get_reported_incidents, 60*1000);

Upvotes: 1

Related Questions