I have code that adds mouseover events and mouseout events to all the 'a' tags on the page. I would like the mouse to start a 5 second timer, after which it calls a function. However, if a new mouseover event occurs, it should cancel any existing timers. The code that I use follows. SetTimeout () works fine, but it seems like clearTimeout () is not referencing the correct timeout, although I declared it globally. Any suggestions?
var timeoutID;
function addMouseoverEvent() {
$('a').each(function(index) {
$(this).mouseover(function() {
clearTimeout(timeoutID);
})
});
}
function addMouseoutEvent() {
$('a').each(function(index) {
$(this).mouseout(function() {
timeoutID = setTimeout(function() {
}, 5000);
})
});
}
$(window).load(function() {
addMouseoverEvent();
addMouseoutEvent();
});
, . , . mouseover, . mouseout - , mouseout.