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
It’s totally personal preference.
setIntervalhas 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
setTimeoutcalls (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
setTimeoutcalls 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. UsingsetInterval, because the ajax calls are asynchronous, you could end up overlapping them.