How to change global var value on div hover.
When I hover on Class .mehover or .mehoverAnother the class 'hideplease' will be added to .mehide or .mehideAnother. When on hoverOut remove the class .mehide or .mehideAnother but delay the removal of a class by 2s and if each time I hover on .mehover or .mehoverAnother change the timetolive variable value to 0.
see my code below:
Javascript
var timetolive = 2000;
$(document).ready(function() {
$('.meHover').hover(function() {
//code here to change the timetolive var value
//the .mehide or .mehideAnother should hide immediately by changing the timetolive var value to 0
$('.mehide').addClass('hideplease');
}, function() {
setTimeout(function(){ $('.mehide').removeClass('hideplease'); }, timetolive); //delay removal of class
});
$('.meHoverAnother').hover(function() {
//code here to change the timetolive var value
//the .mehide or .mehideAnother should hide immediately by changing the timetolive var value to 0
$('.mehideAnother').addClass('hideplease');
}, function() {
setTimeout(function(){ $('.mehideAnother').removeClass('hideplease'); }, timetolive); //delay removal of class
});
});
HTML
<div class="container">
<div class="meHover">hoverme</div>
<div class="meHoverAnother">other hoverme</div>
<div class="mehide"></div>
<div class="mehideAnother"></div>
</div>
jsfiddle sample here https://jsfiddle.net/pqn01e5h/9/
setTimeoutreturns timer object, which you can pass toclearTimeoutfunction to "cancel" it.timetolive = 0;?timetolive = 0;:D?