<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>js的间歇与超时调用</title> </head> <body> <font id="time"></font> </body> </html> <script type="text/javascript"> //注意须要让当前的dom载入全然之后,再运行js代码 var parent = document.getElementById(‘time‘); var time = new Date(); var child = document.createTextNode(‘当前time: ‘+time.toLocaleString()); parent.appendChild(child); //利用setInterval设定相隔一秒更新时间 var id = setInterval(function(){ parent.removeChild(parent.firstChild); var child = document.createTextNode(‘当前time: ‘+(new Date).toLocaleString()); parent.appendChild(child); },1000); //利用setTimeout()延迟某代码的运行。同一时候利用clearTimeout()清除运行计划 //1分钟后取消时间的更新 var timeOut = function(){ //取消时间更新 if(confirm(‘确认不在更新时间?‘)){ clearTimeout(id); }else{ var result = prompt(‘输入随意值继续更新时间:‘,‘请输入參数‘); alert(result); } } setTimeout(‘timeOut()‘,1000*60); </script>
时间: 2024-11-03 01:16:45