定时器体验
<!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" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> <script> //js ,每隔1秒钟,时间自动 变化1秒 window.onload=function() { setInterval(fn,1000) //每隔1000毫秒(1秒)执行一次fn函数 var demo=document.getElementById("demo"); var num=1; //一定写在外面,是全局变量 function fn() { num++; demo.innerHTML=num; } } </script> </head> <body> <div id="demo"></div> </body> </html>