2014-03-04 36 views
-1

我试图在淡入淡出的元素上设置超时时间,所以它在淡入/淡出之前有一点点的缓冲。出于某种原因,我的setTimeout函数似乎不工作。我以前遇到过这个麻烦,我真的很想知道为什么。setTimeout不起作用?

这是我使用什么目前:

var button_timer; 
    if (fade_in_but_hover != "none" && fade_out_but_hover != "none") { 
     $(".first-level-items > .li").hover(function() { 
      clearTimeout(button_timer); 
      $(this).find("a").first().animate({ color: font_color_hover }, fade_in_but_hover); 
     }).mouseleave(function() { 
      button_timer = setTimeout(function() { 
       $(this).find("a").first().animate({ color: font_color }, fade_out_but_hover); 
      }, 1000); 
     }); 
    } 
+0

。李可能是一个错字! –

回答

4

您需要范围this

}).mouseleave(function() { 
     var $this = $(this);//here 
     setTimeout(function() { 
      //use $this here 
      $this.find("a").first().animate({ 
       color: font_color 
      }, fade_out_but_hover); 
     }, 1000); 
    }); 
+1

如果你想在其上使用jQuery方法,它不应该是$ this = $(this)吗? – Mathias

+0

@Mathias谢谢,我总是忘记:) – Anton