不明白的地方有點(diǎn)多,希望前輩們、師哥師姐們能為我解惑,不勝感激!
<!DOCTYPE?HTML>
<html>
<head>
<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8">
<title>計(jì)時(shí)器</title>
<script?type="text/javascript">
???function?clock(){
??????var?time=new?Date();?????????????????????
??????document.getElementById("clock").value?=?time;
???}
//?每隔100毫秒調(diào)用clock函數(shù),并將返回值賦值給i
?????var?i=window.setInterval("clock()",100);
?????
</script>
</head>
<body>
??<form>
????<input?type="text"?id="clock"?size="50"??/>
????<input?type="button"?value="Stop"?onclick="var?inn=window.clearInterval(i)"??/>
??</form>
</body>
</html>上面這段代碼運(yùn)行是沒有問題的。
再來看一段代碼,僅修改了兩個(gè)地方,分別是第13行的:“①var inn=window.clearInterval(i);和第19行的:②<input type="button" value="Stop" onclick="inn" ?/>
”
<!DOCTYPE?HTML>
<html>
<head>
<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8">
<title>計(jì)時(shí)器</title>
<script?type="text/javascript">
???function?clock(){
??????var?time=new?Date();?????????????????????
??????document.getElementById("clock").value?=?time;
???}
//?每隔100毫秒調(diào)用clock函數(shù),并將返回值賦值給i
?????var?i=window.setInterval("clock()",100);
?????var?inn=window.clearInterval(i);
</script>
</head>
<body>
??<form>
????<input?type="text"?id="clock"?size="50"??/>
????<input?type="button"?value="Stop"?onclick="inn"??/>
??</form>
</body>
</html>上面這段代碼就不能正常運(yùn)行了,實(shí)在不知道為什么不能這么來用,當(dāng)然還有一種可能我有寫錯(cuò)的地方,但是我不認(rèn)為這個(gè)可能性很大,因?yàn)槲抑桓牧松厦嫣岬降膬商幍胤健?/p>
再來看一段代碼:
<!DOCTYPE?HTML>
<html>
<head>
<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8">
<title>計(jì)時(shí)器</title>
<script?type="text/javascript">
???function?clock(){
??????var?time=new?Date();?????????????????????
??????document.getElementById("clock").value?=?time;
???}//?每隔100毫秒調(diào)用clock函數(shù),并將返回值賦值給i
?????var?i=setInterval("clock()",100);
</script>
</head>
<body>
??<form>
????<input?type="text"?id="clock"?size="50"??/>
????<input?type="button"?value="Stop"?onclick="clearInterval(i)"??/>
??</form>
</body>
</html>這段代碼倒是可以運(yùn)行,不過我有不理解的地方:onclick="clearInterval(i)"中的clearInterval()應(yīng)該是對(duì)象的一種方法,為什么不用添加上window.也能用。
window.clearInterval(i)
?希望前輩們不吝賜教,在下感激不盡!
2015-04-27
這個(gè)在新的標(biāo)準(zhǔn)中,是不用寫也可以的 。因?yàn)槟J(rèn)會(huì)這么調(diào)用的
2015-05-04
var?i=window.setInterval("clock()",100);
var?inn=window.clearInterval(i);
這兩個(gè)同時(shí)存在的話,剛生成i就被清掉了,所以頁面上不顯示了