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 :)

http://jsfiddle.net/ddbtz/3/

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()

http://jsfiddle.net/ddbtz/8/


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -