vue中轮训器的使用

网络编程 2021-07-04 15:50www.168986.cn编程入门
今天长沙网络推广就为大家分享一篇关于vue中轮训器的使用,长沙网络推广觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随长沙网络推广来看看吧

核心代码:

<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();
    },
    /*
    * 停止轮训
    * */
    sTime() {
     this.timerObj = {}
    },
    /*
    * 要轮训的代码
    * */
    getData(){
     this.log+=1;
     console.log("this.log:"+this.log);
    }
   },
   destroyed(){
    this.sTime();
   }
  }
</script>
<style scoped>
</style>

效果图:

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对狼蚁SEO的支持。如果你想了解更多相关内容请查看狼蚁网站SEO优化相关链接

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by