核心代碼:
<template> <div > {{log}} </div></template><script> export default { name: "TrainingInRotation", data(){ return { log:0, timerId:1, // 模擬計時器id,唯一性 timerObj :{}, // 計時器存儲器 } }, created(){ this.startTraining(); }, methods: { /* * 開始輪訓 * */ 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(); }, /* * 停止輪訓 * */ stopTime() { this.timerObj = {} }, /* * 要輪訓的代碼 * */ getData(){ this.log+=1; console.log("this.log:"+this.log); } }, destroyed(){ this.stopTime(); } }</script><style scoped></style>
效果圖:
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林網的支持。如果你想了解更多相關內容請查看下面相關鏈接
新聞熱點
疑難解答