<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>jsTime.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <script type="text/javascript"> function count(){ setTimeout("alert('hello,world');", 7000); //7秒后执行前面的语句(一次) } </script> </head> <body> <input type="button" value="定时器" onclick="count()"> </body> </html>
第二种方式:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>jsTime2.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script type="text/javascript"> var num = 0; var interval = setInterval("count();",1000); //重复执行表达式 function count(){ document.getElementById("counter").innerHTML = num++; } function stop(){ clearInterval(interval); //终止定时器 alert("停止计时"); } </script> </head> <body> <div id="counter">0</div> <input type="button" value="停止计时" onclick="stop();" > </body> </html>