vue偶爾也會(huì)用到定時(shí)的取更新點(diǎn)內(nèi)容之類的需求乍迄,比如最近遇到的就是每個(gè)約1分鐘更新一下列表的內(nèi)容役电。
主要就是利用created和beforeDestroy這兩個(gè)回調(diào)抄腔,來創(chuàng)建定時(shí)器和清除定時(shí)器改抡。
1售淡、created函數(shù)里面盛险,初始化一下定時(shí)器
這里的onSearch函數(shù)會(huì)每60s被調(diào)用一次,去后臺(tái)獲取內(nèi)容勋又。
created() {
this.timer = setInterval(() => {
this.onSearch(this.pageinfo.params, this.pageinfo.current);
this.closeExcelImportUploadDetailDialog();
}, 1000 * 60);
},
2苦掘、methods里面增加一個(gè)clearTimer的函數(shù)
clearTimer() {
if(this.timer) {
clearInterval(this.timer);
this.timer = null;
}
}
3、最后在beforeDestroy()生命周期內(nèi)清除定時(shí)器
之后就不會(huì)看到每60s調(diào)用一次對(duì)onSearch函數(shù)的調(diào)用
beforeDestroy() {
// debugger;
this.clearTimer();
},