Just found this code-snippet in our application. I am wondering if the first line is superfluous — does one need to call clearTimeout on a variable if it is immediately overwritten? Or is there some condition I should be aware of?
function Countdown() {
clearTimeout(sessionTimeoutHandle);
sessionTimeoutHandle = setTimeout(function () { countdownHandler() }, MILLISECONDS);
}
My hunch is “Yes, you need to call clearTimeout” because I can’t think of why the clearTimeout method would exist if it was OK to just set the timeout variable to null.
I suppose the better question is:
var timeoutHandler = setTimeout(countdownHandler, MILLISECONDS);
timeoutHandler = setTimeout(countdownHandler, MILLISECONDS);
Do I now have two functions waiting to fire in approximately MILLISECONDS, or just one?
No, you only need to call it if you actually need to clear it.
If this is the only code using that variable, then there should be no need.
FYI, it looks like you could shorten the code a bit…
Nothing was gained with the anonymous function.