简单计时
<!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=gb2312" /> <title>简单计时</title> </head> <body> <p>点击按钮,2 秒后弹出 "Hello"。</p> <button οnclick="myFunction()">点我</button> <script> function myFunction(){ setTimeout(function(){alert("swxctx")},2000); } </script> </body> </html> <!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=gb2312" /> <title>无标题文档</title> </head> <body> <script> function timedText(){ var x=document.getElementById('txt'); var t1=setTimeout(function(){x.value="2 秒"},2000); var t2=setTimeout(function(){x.value="4 秒"},4000); var t3=setTimeout(function(){x.value="6 秒"},6000); } </script> </head> <body> <form> <input type="button" value="显示文本时间" οnclick="timedText()" /> <input type="text" id="txt" /> </form> </body> </html> 无穷计数 <!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=gb2312" /> <title>单击按钮一直计数</title> <script> var c=0; var t; var timer_is_on=0; function timedCount(){ document.getElementById('txt').value=c; c=c+1; t=setTimeout("timedCount()",1000); } function doTimer(){ if (!timer_is_on) { timer_is_on=1; timedCount(); } } </script> </head> <body> <form> <input type="button" value="开始计数!" onClick="doTimer()"> <input type="text" id="txt"> </form> </body> </html> <!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=gb2312" /> <title>无标题文档</title> <script> var c=0; var t; var timer_is_on=0; function timedCount(){ document.getElementById('txt').value=c; c=c+1; t=setTimeout(function(){timedCount()},1000); } function doTimer(){ if (!timer_is_on){ timer_is_on=1; timedCount(); } } function stopCount(){ clearTimeout(t); timer_is_on=0; } </script> </head> <body> <form> <input type="button" value="开始计数!" οnclick="doTimer()" /> <input type="text" id="txt" /> <input type="button" value="停止计数!" οnclick="stopCount()" /> </form> </body> </html> 钟表 <!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=gb2312" /> <title>无标题文档</title> <script> function startTime(){ var today=new Date(); var h=today.getHours(); var m=today.getMinutes(); var s=today.getSeconds();// 在小于10的数字钱前加一个‘0’ m=checkTime(m); s=checkTime(s); document.getElementById('txt').innerHTML=h+":"+m+":"+s; t=setTimeout(function(){startTime()},500); } function checkTime(i){ if (i<10){ i="0" + i; } return i; } </script> </head> <body οnlοad="startTime()"> <div id="txt"></div> </body> </html>