写了一个js中的setTimeout函数的例子。这个函数的功能主要是设置一个时间,时间结束之后执行第一个参数代表的js代码。例子如下
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Timeout Example</title>
<script type="text/javascript">
var counter=0;
ID=window.setTimeout("Update()",1000);
function Update(){
counter++;
document.form1.input1.value="The counter is now at "+counter;
ID=window.setTimeout("Update()",1000);
}
</script>
</head>
<body>
<h1>Timeout Example</h1>
<hr>
<p>The text value below is being updated every two seconds.
Reress the RESET button to reset the count,or the STOP button
to stop it.</p>
<hr>
<form name="form1">
<input type="text" name="input1" size="40"/><br />
<input type="button" value="RESET" οnclick="counter=0;"><br/>
<input type="button" value="STOP" οnclick="window.clearTimeout(ID);">
</form>
</body>
</html>
我试图将form元素去掉。input元素设置值那句话改成:document.input1.value="something...";
这时控制台会报错 docum.input1.value is undefined 。看来这里的form1元素是不能缺少的。
功力尚浅。还需练习啊。