How do you rerun (start from 0) a setTimeOut()?
This is what I am trying based on other questions: (it doesn’t work)
var timer;
function talk() {
clearTimeout(timer);
$('#dialog').show();
timer = setTimeout(function(){
$('#dialog').hide();
}, 2000);
}
$("#dialog").hover(function () {
talk();
});
talk();
How can I properly cancel anything on the settimeout and run it again?
You can see how it doesn’t work here: jsfiddle.net/yrBGm
This checks if the hovering is happening. If so, it cancels the timeout and re-runs it later. If not, the timeout continues:
EDIT:
Also, when you defined timer in “var timer;”, timer is undefined, so you can test if it is undefined before you run it in the “timer = setTimeout(funcion () { … like this: