核心代碼:
<template> <div > {{log}} </div></template><script> export default { name: "TrainingInRotation", data(){ return { log:0, timerId:1, // 模擬計時器id,唯一性 timerObj :{}, // 計時器存儲器 } }, created(){ this.startTraining(); }, methods: { /* * 開始輪訓(xùn) * */ startTraining() { let this_ = this; const id = this.timerId++ this.timerObj[id] = true async function timerFn() { if (!this_.timerObj[id]) return await this_.getData(); setTimeout(timerFn, 1 * 1000) } timerFn(); }, /* * 停止輪訓(xùn) * */ stopTime() { this.timerObj = {} }, /* * 要輪訓(xùn)的代碼 * */ getData(){ this.log+=1; console.log("this.log:"+this.log); } }, destroyed(){ this.stopTime(); } }</script><style scoped></style>
效果圖:
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對武林網(wǎng)的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
新聞熱點
疑難解答