jquery - clearTimeout(var) not working as expected -


first: searched , questions cleartimeout not working variable scope problems, not case.

i need auto-hide header after x seconds without interaction, created 2 functions, startmenutimeout , clearmenutimeout(), part of code looks that:

var menutimeout = null; function startmenutimeout(){   menutimeout = settimeout(function(){     $('[auto-header]').removeclass('-visible');   }, 2000); } function clearmenutimeout(){   cleartimeout(menutimeout); } 

when user scrolls up, make header visible , start timeout, then, on mouseenter clear timeout.

problem is, doesn't clear timeout, if scroll , down few times, after 2 seconds timeout, menu goes up.

i reproduced problem on codepen, click here access.

after blazing fast comments of kevin b , carcigenicate solved it. thing is, every time call settimeout(), creates new id, then, if attributing timeout id variable, override, losing reference previous one.

the solution clearing timeout before setting new one, 1 timeout active @ time.

the code looks this:

var menutimeout = null; function startmenutimeout(){   cleartimeout(menutimeout);   menutimeout = settimeout(function(){     $('[auto-header]').removeclass('-visible');   }, 2000); } function clearmenutimeout(){   cleartimeout(menutimeout); } 

click here access updated pen.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -