如何在一段时间后,才执行function? (setTimeout()) (Web) (JavaScript)
1
<!--
2
(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4
Filename : setTimeout.htm
5
Compiler : IE 6.0
6
Description : Run the function after specified timeout
7
Synopsis : iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])
8
Release : 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

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

See Also
如何每间格一段时间就执行function? (setInterval()) (Web) (JavaScript)
Reference
最新JavaScript完整与法参考辞典 3rd P.378, 古旗一浩 着/柯志杰 译, 博硕文化