最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Help chosing between settimeout and setinterval - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Jun 13, 2011 at 15:44 oshirowanen asked Jun 13, 2011 at 15:27 oshirowanenoshirowanen 16k83 gold badges205 silver badges357 bronze badges 5
  • please format your code correctly even if it's as little as this. Thanks! – Trufa Commented Jun 13, 2011 at 15:29
  • do you want to run it on the minute, or just "roughly some time each minute" ? – Alnitak Commented Jun 13, 2011 at 15:39
  • 1 Roughly would be good enough. Accuracy is not a concern in this application. – oshirowanen Commented Jun 13, 2011 at 15:43
  • When you use setInterval, it doesn't execute the mand right away, it waits until the first interval. – gen_Eric Commented Jun 13, 2011 at 15:49
  • 1 about update: setInterval does not call your function directly, first need to wait for the timeout to pass which in this case is 1 min. Also it is not good to use alert's there because you might end with multiple alerts waiting in a stack if you fail to close them before the next function call – venimus Commented Jun 13, 2011 at 15:51
Add a ment  | 

7 Answers 7

Reset to default 3

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 pletes. Using setInterval, because the ajax calls are asynchronous, you could end up overlapping them.

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

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

So, to run get_reported_incidents every minute use setInterval.

var interval = setInterval(get_reported_incidents, 60000);

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./library/blstvsi.htm for a parison.

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

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

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();
发布评论

评论列表(0)

  1. 暂无评论