日期:2014-05-16 浏览次数:20592 次
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script type="text/javascript"> var secs = 5; function Load(){ for(var i=secs;i>=0;i--) { //if(parseInt(i)==0){ //i=5; //}else{ window.setTimeout('doUpdate('+i+')', (secs-i) * 1000); // } } } function doUpdate(num){ document.getElementById('showDiv').innerHTML = '<h2>'+num+'...</h3>'; } </script> </head> <body> <script> window.setTimeout("Load()",1000); </script> <div id="showDiv"></div> </body> </html>
<script type="text/javascript"> var secs = 5; function Load(){ for(var i=secs;i>=-1;i--) { //if(parseInt(i)==0){ //i=5; //}else{ window.setTimeout('doUpdate('+i+')', (secs-i) * 1000); // } } } function doUpdate(num){ document.getElementById('showDiv').innerHTML = '<h2>'+num+'...</h3>'; if(num<0){ Load(); } } </script>
------解决方案--------------------
用递归
------解决方案--------------------
你再仔细看下你注释的那串代码,for的条件永远为真,死循环了。。
如果只是不断的输出倒计时。下面的就够用了
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div id="showDiv"></div>
<script>
var secs = 5;
function Load(){
if(secs == 0){
secs = 5;
}else{
secs--;
}
doUpdate(secs);