Help chosing between settimeout and setinterval

2019-08-10 20:10发布

问题:

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

回答1:

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.



回答2:

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


回答3:

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


回答4:

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.



回答5:

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


回答6:

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".



回答7:

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