JS---setTimeout()与setInterval()的使用

setTimeout(fun,time)表示time毫秒后执行fun函数。

SetInterval(fun,time)表示每隔time毫秒执行fun函数一次。

//-----用setTimeout动态显示当前的时间-----
function showTime(){
  var today=new Date();
  console.log(today.toString());
  setTimeout(showTime,1000); //递归调用   
}
showTime();

//------用setInterval动态显示当前的时间-----
function showTime(){
  var today=new Date();
  console.log(today.toString());
}
setInterval(showTime,1000);
//使用setInterval()制作倒计时
var i=9;
        tout=setInterval(function(){
            console.log("还剩"+i+"秒");
            if(i==0){
                clearInterval(tout);//停止计时
                alert('go!');
            }
            i--;
        },1000);

 setInterval是间隔执行

setTimeout是延迟执行

原文地址:https://www.cnblogs.com/beast-king/p/5292346.html