如何在一段时间后,才执行function? (setTimeout()) (Web) (JavaScript)
1<!--
2(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4Filename : setTimeout.htm
5Compiler : IE 6.0
6Description : Run the function after specified timeout
7Synopsis : iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])
8Release : 11/25/2006
9-->
10<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
11<html xmlns="http://www.w3.org/1999/xhtml">
12<head>
13 <title></title>
14 <script language="javascript" type="text/javascript">
15 function Button1_onclick() {
16 setTimeout("printMsg()",5000);
17 }
18
19 function printMsg() {
20 document.getElementById("div1").innerHTML += "Hello World!!<br>";
21 }
22 </script>
23</head>
24<body>
25 <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
26 <div id="div1">
27 </div>
28</body>
29</html>
2(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4Filename : setTimeout.htm
5Compiler : IE 6.0
6Description : Run the function after specified timeout
7Synopsis : iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])
8Release : 11/25/2006
9-->
10<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
11<html xmlns="http://www.w3.org/1999/xhtml">
12<head>
13 <title></title>
14 <script language="javascript" type="text/javascript">
15 function Button1_onclick() {
16 setTimeout("printMsg()",5000);
17 }
18
19 function printMsg() {
20 document.getElementById("div1").innerHTML += "Hello World!!<br>";
21 }
22 </script>
23</head>
24<body>
25 <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
26 <div id="div1">
27 </div>
28</body>
29</html>
See Also
如何每间格一段时间就执行function? (setInterval()) (Web) (JavaScript)
Reference
最新JavaScript完整与法参考辞典 3rd P.378, 古旗一浩 着/柯志杰 译, 博硕文化