<!DOCTYPE html><html>?<head>? <title>瀏覽器對象</title> ?? <meta http-equiv="Content-Type" content="text/html; charset=gkb"/> ???</head>?<body>? <!--先編寫好網(wǎng)頁布局-->? <h3>操作成功</h3>? <p><span id="test"></span>秒后回到主頁 <a href="javascript:back()">返回</a></p>???? <script type="text/javascript"> ??? ?//獲取顯示秒數(shù)的元素,通過定時器來更改秒數(shù)。?var num=5;function clock(){? ?? ? document.getElementById("test").innerHTML=num;? ? num-=1;?? ?if(num==0){? ? ? ?location.assign("http://idcbgp.cn");? ?}? ? ? //setTimeout("clock()",1000);}setInterval("clock()",1000);//setTimeout("clock()",1000);? ?//通過window的location和history對象來控制網(wǎng)頁的跳轉(zhuǎn)。? function back(){? ? ? window.history.back();? }??</script>?</body></html>
為什么計時器setTimeout 要使用兩次,函數(shù)內(nèi)外個一次,而setInterval則只要在函數(shù)外使用一次?
摩羯座D羊
2017-07-15 23:10:02