1、最简单的用法,500毫秒后执行,执行结束,定时器自动清除。
setTimeout(function() {
// 需要执行的代码
}, 500); // 定时时间
2、循环执行定时器
data() {
return {
// 定时器
monitorUser: "", // 接收定时器id,便于清除定时器
}
},
onHide() {
// 清除定时器
clearTimeout(that.monitorUser);
},
onShow() {
// 执行定时器
this.testTimeOut();
}
methods: {
定时器方法
testTimeOut(){
// 执行逻辑代码
// 使用递归法定时执行定时器
this.monitorUser = setTimeout(this.testTimeOut,2000);
},
}