2014-04-05 22 views
0

我有以下代碼:如何在jQuery中使用「setTimeout」?

<head> 
    <script> 
    $(document).ready(function() { 
     window.setTimeout(function() { 
     $("#Error").animate({margin:"-50px"}); 
     $("#errorCloseholder").animate({margin:"-50px"}); 
     }, 2000); 
    }); 
    </script> 
</head> 

此代碼,用jQuery的協助下,應該隱藏2秒後的一些元素。

不幸的是,這段代碼無法正常工作。它是否與jQuery有關?或者我有某種語法錯誤?

元素的ID爲Error和errorCloseholder,它們都是要在2秒後隱藏的錯誤消息。

在此先感謝。

+1

開始與此'});'。 – Incognito

+0

^哈哈它是注意只需按下鍵盤上1左邊的「那個按鈕」。 – Incognito

+0

這裏你不需要'window.'前綴;提到,出於好奇:爲什麼你在這裏使用超時,而不是一個CSS類的轉換延遲? –

回答

0
$(document).ready(function() { 
    setTimeout(function(){ 
    $("#Error").animate({margin: "-50px"}); 
    $("#errorCloseholder").animate({margin: "-50px"}); 
    }, 2000); 
}); 

刪除';'從您的腳本。

2

在您的.ready()函數結尾處出現語法錯誤。

 };); //this one remove the first semi colon 
    </script> 
</head> 

An example

+0

問題是在眨眼---- - >;) – Su4p

+0

你如何在註釋中放置代碼標籤? – Su4p

+0

^哈哈它是注意只需按下鍵盤上1左側的「那個按鈕」。 – Incognito

0

嘗試使用此​​事件......

$(document).ready(function() { 
    $(window).load(function(){ 
    setTimeout(function(){ 
     $("#Error").animate({margin:"-50px"}); 
     $("#errorCloseholder").animate({margin:"-50px"}); 
    }, 2000); 
    }); 
}); 
+0

什麼 - > $(文檔).ready(函數(){ $(窗口).load(函數(){爲什麼? – Su4p

+0

我認爲動畫將在資源獲取加載前完成..所以用戶無法看到它.. –

+0

$(document).ready(//不夠? – Su4p

0

看起來你有一個太多的分號。嘗試刪除「.ready」函數中的倒數第二個,看看是否有幫助。

0

您可以使用setTimeout()方法在定義的no後調用函數。毫秒。

$(document).ready(function() { 
    var x = document.getElementById("txt"); 

    setTimeout(function(){x.value="2 seconds"},2000);<br/> 
    setTimeout(function(){x.value="4 seconds"},4000);<br/> 
    setTimeout(function(){x.value="6 seconds"},6000);<br/> 
} 

在這裏,「txt」是標籤或文本框。 結果將是這樣的:
2秒
4秒
6秒