概述
Definition and Usage
The setTimeout() method is used to call a function or evaluate an expression after a specified number of milliseconds.
Syntax
setTimeout(code,millisec,lang) |
Parameter | Description |
---|---|
code | Required. A pointer to a function or the code to be executed |
millisec | Required. The number of milliseconds to wait before executing the code |
lang | Optional. The scripting language: JScript | VBScript | JavaScript |
<html>
<head>
<script type="text/javascript">
function timedMsg()
{
var t=setTimeout("alert('5 seconds!')",5000)
}
</script>
</head>
<body>
<form>
<input type="button" value="Display timed alertbox!"
onClick="timedMsg()">
</form>
<p>Click on the button above. An alert box will be
displayed after 5 seconds.</p>
</body>
</html>
setTimeout()函式讓你可以每隔一段時間自動呼叫另一組函式, 最常見的就是做時鐘了.
<script language=Javascript> function hello() { alert("hello"); setTimeout("hello();", 2000); } </script> <Body οnlοad=hello();> |
阿得針對以上的程式碼講解一下:
function hello(),這是Javascript函式的寫法,算是複習囉
alert(),如果您現在還不是很清楚這個是什麼,您應該去查一下先前的教學
SetTimeout("hello();",2000), 這張就是王牌了,SetTimeout這個函式名稱大小寫有差,如果打錯了不管怎麼試都試不出來喔!!後面""中的hello()是函式名稱,請注意一定 要有引號!!而逗點之後的數字代表在多少時間內執行此函式,單位為千分之一秒。整段程式碼的解釋就是兩秒鐘後會再執行一次hello()。
Body οnlοad=hello();,這個是呼叫函式,因為SetTimeout是寫在hello()裡面,而hello()是一個函式,如果您一開始載入網頁沒有呼叫函式那函式就不會有作用。
了乎﹖其實不是很難,就是說您執行了hello(),而hello函式會跳出提示視窗說hello,再跳出視窗後又設定再兩秒後重新執行hello(),這樣一來不是每兩秒都會跳出一個說hello的提示視窗嗎﹖簡單吧!
不知道您有沒有看過那種狀態列跑馬燈,那種其實是用setTimeout,在很快的時間內在文字前面(或是後面)一次一次加上空白而將該段文字擠出狀態列,讓人感覺文字在移動(因為改變的速度太快了,好像慢慢在移動)。
最后
以上就是彪壮便当为你收集整理的 setTimeout()隔一段时间后弹出警告信息的全部内容,希望文章能够帮你解决 setTimeout()隔一段时间后弹出警告信息所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复