Skip to content

Instantly share code, notes, and snippets.

@gavinwhyte
Last active July 29, 2021 23:22
Show Gist options
  • Select an option

  • Save gavinwhyte/c3dc38f0816a9ee998a1eb8abde813ba to your computer and use it in GitHub Desktop.

Select an option

Save gavinwhyte/c3dc38f0816a9ee998a1eb8abde813ba to your computer and use it in GitHub Desktop.
vuejs polling

data() {

return { status: null, pollInterval: null }

},

...

methods() { fetchData() { axios.get('r/http://api/process/status') .then((response) => { //check if status is completed, if it is stop polling if(response.data.status = 'completed') { clearInterval(this.pollInterval) //won't be polled anymore } this.status = response; }); } },

mounted() { this.fetchData() //check if the status is completed, if not fetch data every 10minutes if(this.status.status != 'completed') { this.pollInterval = setInterval(this.fetchData(), 600000) //save reference to the interval setTimeout(() => {clearInterval(this.pollInterval}, 36000000) //stop polling after an hour } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment