window对象: setTimeout setInterval和clearInterval

setTimeout

setTimeot(
  代码块,毫秒
);

例子

seTimeout(function(){
   consloe.log("2s到了");
 },2000);

2000代表的是毫秒(1秒等于1000毫秒)

毫秒前面要加“,”

setInterval

setInterval(
  代码块,毫秒
);

例子:

setInterval(function(){
    count++;
    console.log("Yong"+count);
},1000);

此函数表示每隔多长时间执行一次

clearInterval

var count = 0;
var timer = setInterval(function(){
    count++;
    if(count>3){
        cleraInterval(timer)
    }
},1000);    

此案例每一秒执行一次,当执行到第三次的时候会停止

原文地址:https://www.cnblogs.com/fushengfuge/p/8496655.html