js

使用js处理问题的时候,我们可能会经常使用到setInterval()来进行定时任务或者轮询的操作,那么如何让setInterval停止和重新启动呢,下边的代码就可以实现的呦,如果有更好的方法,不吝赐教。

// 定义定时任务
function func(){console.log("hello")} 
//启动定时器,func是参数,所以不能使用括号
var interval = setInterval(func, 2000); //启动,func不能使用括号
// 停止定时器
clearInterval(interval );
//重新启动即可
interval = setInterval(func, 2000); 

转载于:https://www.cnblogs.com/icesnow521/p/9081216.html

原文地址:https://www.cnblogs.com/500m/p/13614856.html