Sometimes in setTimeout functions you want to make sure it doesn't already exist before executing an action.
Upon execution of a setTimeout, is there any value in clearing/re-assigning the variable containing the timeout ID? What about setInterval?
You'll notice in my example timer1 executes 2 logs, whereas timer2 does not.
function renderUtility(name, value) {
var el = document.createElement('p');
el.textContent = name + ": " + value
document.body.appendChild(el);
}
var timer1 = setTimeout(function() {
clearTimeout(timer1);
renderUtility('timer1', timer1);
if (timer1) renderUtility('timer1', "I exist");
}, 1000);
var timer2 = setTimeout(function() {
clearTimeout(timer2);
timer2 = null;
renderUtility('timer2', timer2);
if (timer2) renderUtility('timer2', "I exist");
}, 1000);