[Vuejs]-Why this setInterval is executing multiple times?

0👍

A better design is to wrap setTimeout with a promise, and do the polling in an async method that loops…

mounted: function() {
  this.continuePolling = true;  // suggestion: we have to stop sometime.  consider adding continuePolling to data
  this.poll();
},

unmounted: function() {         // almost the latest possible stop
  this.continuePolling = false;
},

methods:
  async poll(interval) {
    const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
    while(this.continuePolling) {  
      await this.updateProgress();
      await delay(7000);
    }
  },

  async updateProgress() {
    const id = this.$route.params.id
    const progOut = await this.api.get(`/api/mu/job/${id}/status`)
    const result = progOut.data.data;
    this.progress = result.progress / 100
    this.state = result.status
  }

Leave a comment