2015-12-16 17 views
0

下面的代碼位因某些原因無法工作,它不喜歡的setTimeout的一部分,而不是唯一的工作版本我能做的就是不延遲...爲什麼我的每個類使用的jQuery代碼片段不支持setTimeout?

jQuery(".divhere").hide(); 
if(jQuery('.divhere').length >= 1){ 
    jQuery(".divhere").each(function() { 
     setTimeout(function(el) { 
      jQuery(this).slideDown("slow"); 
      jQuery(this).show(); 
     }, 1000); 
    }); 
} 

只有一個工作是:

jQuery(".divhere").hide(); 
if(jQuery('.divhere').length >= 1){ 
    jQuery(".divhere").each(function() { 
     jQuery(this).slideDown("slow"); 
     jQuery(this).show(); 
    }); 
} 
+2

裏面的setTimeout的處理程序'this'指窗口對象 –

回答

1

this上下文中setTimeout情況下被錯過。如果您嘗試console.log(this)裏面setTimeout,您會發現window對象。

使用.bind:JavaScript的綁定允許我們設置的方法

的這個值試試這個:

jQuery(".divhere").hide(); 
if(jQuery('.divhere').length >= 1){ 
    jQuery(".divhere").each(function() { 
     setTimeout(function(el) { 
      jQuery(this).slideDown("slow"); 
      jQuery(this).show(); 
     }.bind(this), 1000); 
    }); 
} 
相關問題