1、开启定时器
// 时间工具库,方便时间格式化
import * as moment from 'moment';
timer = null;
this.timer = setInterval(() => {
console.log(moment(new Date()).format('YYYY-MM-DD HH:mm:ss') + ' 定时器正在执行...');
}, 5000);
2、关闭定时器
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
3、控制台打印信息
2020-11-21 12:44:29 定时器正在执行...
2020-11-21 12:44:34 定时器正在执行...
2020-11-21 12:44:39 定时器正在执行...
2020-11-21 12:44:44 定时器正在执行...
2020-11-21 12:44:49 定时器正在执行...