為什么我的跳轉(zhuǎn)時間直接從還剩4s 到2s 到 -2s 然后才跳轉(zhuǎn)的啊?!
<!DOCTYPE?html> <html> ?<head> ??<title>瀏覽器對象</title>?? ??<meta?http-equiv="Content-Type"?content="text/html;?charset=gkb"/>??? ?</head> ?<body> ??<!--先編寫好網(wǎng)頁布局--> ??<p><h1>操作成功</h1></p> ??<h8?id="seconds"></h8>秒后回到主頁<a?href="windback()">返回</a> ??<script?type="text/javascript">?? ???//獲取顯示秒數(shù)的元素,通過定時器來更改秒數(shù)。? ????var?num?=?5; ????function?timeCount(){ ?????document.getElementById('seconds').innerHTML?=?num; ?????num?=?num-1?; ?????if(num?==?0?){ ????????window.open('http://idcbgp.cn','_self');? ?????}else{ ????????setInterval("timeCount()",1000); ?????} ????} ????setTimeout("timeCount()"); ????/*function?windturn(){ ????????window.open('http://idcbgp.cn','_self'); ????}*/ ???? ????function?windback(){ ????????window.history.back(); ????} ???//通過window的location和history對象來控制網(wǎng)頁的跳轉(zhuǎn)。 ??? ?</script>? </body> </html>
為什么我的跳轉(zhuǎn)時間直接從還剩4s 到2s 到 -2s 然后才跳轉(zhuǎn)的,而不是按順序5 4 3 2 1 0 然后跳轉(zhuǎn)?
2015-07-30
是的,
2015-07-24
我已經(jīng)知道了,把第20行setInterval("timeCount",1000)改為setTimeout("timeCount()",1000)就可以了!因為setTimeout要立即執(zhí)行,而setInterval是間隔固定時間執(zhí)行,這與程序本身的執(zhí)行時間不一致,就容易出現(xiàn)我那種情況