javascript - clearTimeout not working in nested function -
i think it's got nested functions, need way. why isn't working? doing stupid? isolated example , must using $(this), seems have nest functions?
html:
<div class="box"></div> <ul> <li>hover box, turns blue. leave box, turns red after 2 secs.</li> <li>if hover onto box before 2 secs up, it's supposed clear timer , keep box blue.</li> <li>it doesn't clear timer , after 2 secs box still turns red. why?</li> </ul> javascript:
var t; $('.box').on('mouseenter', function() { $thisbox = $(this); cleartimeout(t); $thisbox.addclass('blue'); $thisbox.on('mouseleave', function() { t = settimeout(function() { $thisbox.removeclass('blue'); }, 2000); }) }); jsfiddle: http://jsfiddle.net/ddbtz/7/
thanks looking :)
your .on() shouldn't nested. effectively, that's attaching new handler every time hover on element.
edit: per question clarification.
use .one() instead of .on()
Comments
Post a Comment