Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to clear setTimeouts after they've run?

Simple question really, I'm running a bunch of timeouts but wanna make sure they don't slow the page down and that for some reason they aren't kept in memory after they've executed.

$projects.each(function(index) {
    var $this = $(this);
    window.setTimeout(function() {
        // animate
    }, 300 * index);
});
// Clear timeouts?

My guess is that they're destroyed once they've run but just want to follow best practice.

like image 926
Marko Avatar asked Oct 10 '22 17:10

Marko


1 Answers

No, you don't. Interval timers (via "setInterval()"), yes, if you want them to stop.

It's harmless to clear a timeout that doesn't have to be cleared. That is, if you do clear one after it has run, browsers won't complain.

like image 195
Pointy Avatar answered Oct 20 '22 06:10

Pointy