setInterval 和 setTimeout 用法
阿新 • • 發佈:2018-02-27
setInterval 和 setTisetInterval 定時器,開始執行後,每間隔指定時間執行一次,除非清除定時器
用法: setInterval(function(){ 方法... },1000);// 間隔時間
用法: setInterval(function(){ 方法... },1000);// 間隔時間
setTimeout 是在指定的時間後,執行該事件
用法: setTimeout(function(){
方法....
},1000);
clearInterval(); // 清除定時器
<html>
<head>
<title>js</title>
</head>
<body>
<script>
test(); function test(){ var i = 0; var num = setInterval(function(){ i++; if(i==5){ clearInterval(num); // 當執行到i==5 時,清除定時器 } document.write(i); },1000); } setTimeout(function(){ document.write("hello,world"); },6000); // 為了區分和上面的效果 ,把這個延長久一點 </script> </body>
</html>
setInterval 和 setTimeout 用法