时钟倒计时:
<html> <head> <title>时钟、倒计时</title> <script language="JavaScript"> function rtime(){ var djs = document.getElementById("count").innerText; if(djs!=0){ document.getElementById("count").innerText=djs-1; setTimeout("rtime()",100); //调取函数,调取频率毫秒 只调一次 } } function time(){ var now = new Date(); var hour = now.getHours(); var min = now.getMinutes(); var second = now.getSeconds(); var apm = "AM"; if(hour>12){ hour=hour-12; apm = "PM"; } if(hour<10){ hour="0"+hour; } if(min<10){ min="0"+min; } if(second<10){ second="0"+second; } document.form1.myclock.value = hour+":"+min+":"+second+" "+apm ; setTimeout("time()",1000); } </script> </head> <body onload="time()"> <form name="form1"> <input type="text" name="myclock"> <div id="count" >111</div> <input type="button" value="倒计时开始" onclick="rtime()"> </form> </body> </html>
第一种:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>counter</title> <script language="javascript" type="text/JavaScript"> function Counter(nMax,nInterval) { this.maxTime=nMax; this.interval=nInterval; this.objId="timer"; this.obj=null; this.num=this.maxTime; this.timer=null; this.start=function() { this.obj=document.getElementById(this.objId); if(this.num>0) setTimeout(this.run,this.interval*1000); }; this.run=function() { if(myCounter.num>0) { myCounter.num--; myCounter.obj.innerHTML=myCounter.num; myCounter.timer=setTimeout(myCounter.run,myCounter.interval*1000); } else clearTimeout(myCounter.timer); }; this.show=function() { document.write("<span id="+this.objId+">"+this.num+"</span>"); this.obj=document.getElementById(this.objId); //alert(this.obj.innerHTML); } } </script> </head> <body onload="myCounter.start();"> <script language="JavaScript" type="text/JavaScript"> var myCounter=new Counter(10,1); </script> <p>现在剩下 <script language="JavaScript" type="text/JavaScript"> myCounter.show(); </script> 秒钟!</p>
第二种:
请等待<span id="dd">10</span>秒 <script type="text/javascript"> function run(){ var s = document.getElementById("dd"); if(s.innerHTML == 0){ window.location.href='www.baidu.com'; return false; } s.innerHTML = s.innerHTML * 1 - 1; } window.setInterval("run();", 1000); </script>
第三种:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script type="text/javascript"> <!-- var duration=9900; var endTime = new Date().getTime() + duration + 100; function interval() { var n=(endTime-new Date().getTime())/1000; if(n<0) return; document.getElementById("timeout").innerHTML = n.toFixed(3); setTimeout(interval, 10); } window.onload=function() { setTimeout("window.location.href='http://www.baidu.com'", duration); interval(); } //--> </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>等待10秒</title> </head> <body> <form id="form1" runat="server"> <div> 现在剩下 <span id="timeout">10.000</span> 秒后 将自动跳转 </div> </form> </body> </html>
定时刷新时间:
<html> <head> <title>定时刷新时间</title> <script language="JavaScript"> //<script language="JavaScript" src="js.js"> function time(){ var now =new Date().toLocaleString(); alert(now); } function start(){ timmerID=window.setInterval("time()",1000); //调取函数,调取频率毫秒 } function stop(){ window.clearInterval(timmerID); //只有遇到clearInterval() 才停止 } </script> </head> <body> <input type ="button" value="时间" onclick="time()"></button> <input type ="button" value="启动" onclick="start()"></button> <input type ="button" value="停止" onclick="stop()"></button> </body> </html>