2015-04-26 82 views
-4

我想要做以下使用純JavaScript是否有可能(或jQuery的)警報味精一次,第5秒後加載

  1. 的頁面完全加載。
  2. 等5秒鐘。
  3. alert('msg')once with no setInterval loops。
+2

的downvotes是缺乏研究努力的結果,因爲這不應該是很難研究自己,至少拿出一個代碼嘗試 – charlietfl

回答

1

嘗試setTimeout

document.onload = setTimeout(function() { alert('msg'); }, 5000); 
+1

不會立即執行setTimeout,然後在文檔加載時使用setTimeout的返回值(在這種情況下)不做任何事情嗎?應該是'document.onload = function(){setTimeout(function(){alert('msg');},5000);};'? – dewd

+0

是的,它立即執行'setTimeout',但回調函數不會執行5秒鐘。你想保持返回值的地方? –

+0

我不是OP,但OP表示在文檔加載後5秒後顯示警報。我知道這會在瀏覽器遇到document.onload表達式5秒後顯示警報,而不是在文檔加載後5秒。 – dewd

0

試試這個:

$(document).ready(function(){ 
    setTimeout(function(){ alert("msg");}, 5000) 
}) 

不要忘了把jQuery的參考您的HTML頭:

<script src="http://code.jquery.com/jquery-2.1.3.min.js"></script> 
2

jQuery的解決方案:

$(document).ready(function() 
 
{ 
 
    setTimeout(function() 
 
    { 
 
     alert('msg'); 
 
    }, 
 
    5000); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

使用setTimeout而不是setInterval。我的答案與問題一樣冗長。

+1

頁面後執行滿載包括圖像使用'$(window).ready()'。 – dewd

+0

好的,會更新答案。乾杯 – Luke