一:每隔五秒打印 0-10
var index = 0;
setInterval(function () {
//如果大于10, 则重新赋值为0
if (index > 10) {
index = 0
}
console.log(index);
index++;
}, 5000);
二:每隔五秒 调用一次后台接口
window.setInterval(() => {
setTimeout(() => {
this.getTableInfo(); //获取表格数据
}, 0)
}, 5000 );
三:在需要定时器的方法或生命周期函数中声明并销毁
test() {
let timer = setInterval(() => {
console.log(33);
}, 1000);
this.$once('hook:beforeDestroy', () => clearInterval(timer));
}
四:js清除所有定时器(包括未知定时器)
let end = setInterval(function () { }, 10000);
for (let i = 1; i <= end; i++) {
clearInterval(i);
}
js清除所有定时器(包括未知定时器)