vue封装定时器

| |
[不指定 2023/09/07 13:29 | by 刘新修 ]

main.js 文件

JavaScript代码
  1. /*** sleep ***/  
  2. Vue.prototype.$sleep = time => {  
  3.   return new Promise((resolve, reject) => {  
  4.     window.setTimeout(() => {  
  5.       resolve(true)  
  6.     }, time)  
  7.   })  
  8. }  

增加测试页面

XML/HTML代码
  1. <!--  
  2. * @Description: npage.vue  
  3. * @Version: 1.0  
  4. * @Author: Jesse Liu
  5. * @Date: 2022-08-01 20:32:06  
  6. * @LastEditors: Jesse Liu  
  7. * @LastEditTime: 2023-09-07 13:24:11  
  8. -->  
  9. <template>  
  10.   <div class="wscn-http404-container">  
  11.       <h2 style="font-weight: normal; line-height: 160%;">userInfo ========= {{ userInfo }}</h2>  
  12.       <h2 style="font-weight: normal; line-height: 160%;">configInformation ========= {{ configInformation }}</h2>  
  13.       <h1 style="line-height:300px; text-align:center">npage.vue (新测试文件)</h1>  
  14.       <div>time:</div>  
  15.   </div>  
  16. </template>  
  17.   
  18. <script>  
  19. import { mapState } from "vuex";  
  20. import { parseTime } from '@/utils'  
  21. import { downloadFile } from "@/utils/getFile";  
  22. export default {  
  23.   name: 'PageTest',  
  24.   computed: {  
  25.     ...mapState("user", ["userInfo","configInformation"]),  
  26.     message() {  
  27.       return 'PageTest'  
  28.     },  
  29.     parseTime: parseTime  
  30.   },  
  31.   mounted() {  
  32.   
  33.     this.$store.dispatch("case/casesList/downloadFile",{}).then((res)=>{  
  34.         // let data = res.data;  
  35.         // let headers = res.headers;  
  36.           
  37.         // /*** 调用下载文件 ***/  
  38.         // downloadFile(data, headers)  
  39.     })  
  40.   
  41.     /*** 多定时器同时调用 ***/  
  42.     this.getStateA();  
  43.     this.getStateB();  
  44.   
  45.     /*** 业务请求接口调用示例 ***/  
  46.     this.casePage();  
  47.   },  
  48.   methods: {  
  49.     /*** 定时器演示方法 ***/  
  50.     async getStateA(){  
  51.   
  52.         /*** 定时器只管计算时间钩子标记 ***/  
  53.         console.log('sleepStateA ~~~ 开始')  
  54.   
  55.         /*** 多定时处理任务A ***/  
  56.         console.time('sleepStateA');  
  57.         let sleepStateA = await this.$sleep(5000);  
  58.         console.log('sleepStateA ~~~ 结束', sleepStateA)  
  59.         console.timeEnd('sleepStateA');  
  60.   
  61.         return sleepStateA;  
  62.     },  
  63.     /*** 定时器演示方法 ***/  
  64.     async getStateB(){  
  65.   
  66.         /*** 定时器只管计算时间钩子标记 ***/  
  67.         console.log('sleepStateB ~~~ 开始')  
  68.   
  69.         /*** 多定时处理任务B ***/  
  70.         console.time('sleepStateB');  
  71.         let sleepStateB = await this.$sleep(10000);  
  72.         console.log('sleepStateB ~~~ 结束', sleepStateB)  
  73.         console.timeEnd('sleepStateB');  
  74.   
  75.         return sleepStateB;  
  76.     },  
  77.   
  78.     /*** 模拟示例业务的状态 ***/  
  79.     async getCaseState(){  
  80.   
  81.         /*** 定时器只管计算时间钩子标记 ***/  
  82.         let sleepStateCase = await this.$sleep(10000);  
  83.         return sleepStateCase;  
  84.     },  
  85.     /*** 模拟示例业务主方法 ***/  
  86.     async casePage(state = true, n = 0){  
  87.   
  88.         /*** 定义及获取数据状态 ***/  
  89.         let runling = true;  
  90.         let getCaseState = await this.getCaseState();  
  91.   
  92.         /*** 业务逻辑处理及使用 ***/  
  93.         if(state && getCaseState) {  
  94.             /*** 请求接口拿状态和后端约定状态码(根据状态码去更改runling的布尔值) ***/  
  95.             // 写你的vuex调用方法,如:this.$store.dispatch('user/record', {})  
  96.   
  97.   
  98.             /*** 以下代码是为了演示模拟3次请求后,状态变更,供参考 ***/  
  99.             /*** 假设后端接口告诉你可以终止执行调用的状态 (比如后端调用3次后就更改了状态) ***/  
  100.             let index = n;  
  101.                 index +=1;  
  102.                 console.log('第['+ index + ']次调用后端接口~~');  
  103.   
  104.                 if(index >= 3) {  
  105.                   runling = false;  
  106.                 }  
  107.             this.casePage(runling, index);  
  108.         }  
  109.         else{  
  110.             console.log('===========================================\n定时器已经结束关停,累计调用共: ' + n + '次~~');  
  111.         }  
  112.     }  
  113.   
  114.   }  
  115. }  
  116. </script>  
  117.   
  118.   
  119. <style lang="scss" scoped>  
  120. </style>  

 

H5/JS/CSS | 评论(0) | 引用(0) | 阅读(437)