Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
Why this setInterval is executing multiple times?

I have the below code in a vue application

  mounted: function () {
    this.timer = setInterval(async () => {
      if (this.progress >= 1) {
        this.progress = 1
        clearInterval(this.timer)
      }
      console.log('update')
      const id = this.$route.params.id
      const progOut = await this.api.get(`/api/mu/job/${id}/status`)
      const response = progOut.data
      this.progress = response.data.progress / 100
      this.state = response.data.status
    }, 7000)
  },

I was expecting it to execute the get request every 7 seconds but it is executing the call every 500ms approx

I read other answers and so far I think this is the proper way but the code is executing too many requests

What is the proper way to call a function from within the setInterval to make it actually wait the timeout?

Edit: This was my final code in case someone goes through the same

  methods: {
    redirect (page) {
      if (page === 'FINISHED') {
        this.$router.push({
          name: 'viewReport',
          params: { id: 4 }
        })
      } else {
        this.$router.push({
          name: 'errorOnReport',
          params: { id: 13 }
        })
      }
    }
  },
  watch: {
    state: async function (newVal, old) {
      console.log('old ' + old + ' newVal ' + newVal)
      if (newVal === 'FAILED' || newVal === 'FINISHED') {
        this.redirect(newVal)
      }
    }
  },
  data () {
    return {
      state: null,
      timer: null,
      progress: 0.0,
      progressStr: '0%'
    }
  },
  mounted () {
    const update = async () => {
      if (this.progress >= 1) {
        this.progress = 1
      }
      console.log('update ' + new Date())
      const id = this.$route.params.id
      const progOut = await this.api.get(`/api/mu/job/${id}/status`)
      const response = progOut.data
      this.state = response.data.status
      this.progress = response.data.progress / 100
      this.progressStr = response.data.progress + '%'
    }
    update()
    this.timer = setInterval(update, 10000)
  },
  beforeUnmount () {
    clearInterval(this.timer)
  }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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
  }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!