Best practice: should I clear all pending timeouts and intervals onunload

N

news

A lot of the interactive pages I'm coding at the moment for our
intranet use setInterval to scan for things that need updating,
request data from the server and then render that data back to the
page.

As such, I've often got 3 or 4 intervals running with different time
spacing, adjusted to get the best response without hammering the
server for updates.

Now - should I clear all these intervals in response to an unload
event, or should I let the browser take care of this?

I've always cleared them diligently (prompted by the fact that there
is a function for clearing them), but I realised this morning that
I've never actually read anywhere that this definitely is or is not
necessary.

As a side question, is there any difference between clearTimeout and
clearInterval - in other words do you have to make sure you clear a
reference returned by setTimeout with clearTimeout and so on?
 
T

Thomas 'PointedEars' Lahn

As such, I've often got 3 or 4 intervals running with different time
spacing, adjusted to get the best response without hammering the
server for updates.

I daresay that's broken as designed on your part, see the recent discussions
about setInterval().
Now - should I clear all these intervals in response to an unload
event,

Yes, you should.
or should I let the browser take care of this?

Experience tells that browsers tend to be negligent when it comes to
cleaning up (an example are circular references with host objects).
Furthermore, it would not be completely unreasonable for a user agent to
execute code as long as the execution context it was defined in was not
fully destroyed. I have seen examples of Internet Explorer doing so with
uncleared setInterval(); the result was either a runtime error or other
previously unexpected behavior.
As a side question, is there any difference between clearTimeout and
clearInterval - in other words do you have to make sure you clear a
reference returned by setTimeout with clearTimeout and so on?

You better do, as the reference material says so. Whether the
implementation uses much the same code for both would seem irrelevant.


PointedEars
 
N

news

I daresay that's broken as designed on your part, see the recent discussions
about setInterval().
If I've got the right discussion, that was about using several
setIntervals on the same set of objects to do animation. I'm
using several intervals to regularly fetch jobs from different
job queues - one interval scans for things that need updating,
one interval submits queued http requests and one picks up render
jobs to put the data back on the screen. The key thing is that
it doesn't matter WHEN the intervals fire, just that they do
so regularly.
Yes, you should.

Thought as much. My reason for asking was that clearing
them all was just something I'd always done without having
a clear reason for it and I'm a bit wary of that sort of
code :) If you've experienced problems, that's all the
confirmation I need. Since one of the intervals is making
async http requests, I'd hate to think what would happen if one
triggered as the page was being destroyed leaving the
callback dangling.
You better do, as the reference material says so. Whether the
implementation uses much the same code for both would seem irrelevant.
True - looking at firefox, the value returned by setTimeout and
setInterval is a number and the two never share numbers, so it's
almost definitely being handled by the same code. But as you say,
second guessing the browser is never good practice.

Thanks,
Mike
 

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top