js 定时器

//定时器间隔0.5s,启动定时器后,等待3s

refreshCount=function() {
      var time = 500;
      var interval; //调度器对象。
      function fun(){
        console.log("获取进度");
      }
      interval = setInterval(fun,time);//去掉引号就对了
      return interval
    }

x=refreshCount();
setTimeout(function(){clearInterval(x);console.log("结束定时器!");}, 3000);

输出

"获取进度"
"获取进度"
"获取进度"
"获取进度"
"获取进度"
"获取进度"
结束定时器!
原文地址:https://www.cnblogs.com/sea-stream/p/10765765.html