(原創) 如何每间格一段时间就执行function? (Web) (JavaScript)
1
<!--
2
(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4
Filename : setInterval.htm
5
Compiler : IE 6.0
6
Description : Run the function repeatedly after specified interval
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
setInterval("printMsg()",1000);
18
}
19
20
function printMsg() {
21
document.getElementById("div1").innerHTML += "Hello World!!<br>";
22
}
23
</script>
24
</head>
25
<body>
26
<input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
27
<div id="div1">
28
</div>
29
</body>
30
</html>
<!-- 2
(C) OOMusou 2006 http://oomusou.cnblogs.com3

4
Filename : setInterval.htm5
Compiler : IE 6.06
Description : Run the function repeatedly after specified interval7
Synopsis : iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])8
Release : 11/25/20069
-->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
setInterval("printMsg()",1000);18
}19
20
function printMsg() {21
document.getElementById("div1").innerHTML += "Hello World!!<br>";22
}23
</script>24
</head>25
<body>26
<input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />27
<div id="div1">28
</div>29
</body>30
</html>
See Also
如何在一段时间后,才执行function? (Web) (JavaScript)
(原創) 如何使用Timer? (.NET) (C#) (C++/CLI)
Reference
最新JavaScript完整与法参考辞典 3rd P.378, 古旗一浩 着/柯志杰 译, 博硕文化

浙公网安备 33010602011771号