Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue.js auto reload / refresh data with timer

(New to Vue.js) I fetch data from a get request to display calendar information. I want this to update every 5 minutes.

Nothing in the docs about auto reload - how would I go about implementing this? Do I use standard javascript within the file or something else?

My complete app.js below:

Vue.component('events', {     template: '#events-template',      data: function() {         return {             list: []         }     },      created: function() {          this.fetchEventsList();     },      methods: {          fetchEventsList: function() {              this.$http.get('events', function(events) {                  this.list = events;              }).bind(this);          }      }  });  new Vue({     el: 'body',   }); 
like image 354
Mike Thrussell Avatar asked Apr 12 '16 11:04

Mike Thrussell


People also ask

Can you force Vue js to reload Rerender?

The best way to force Vue to re-render a component is to set a :key on the component. When you need the component to be re-rendered, you just change the value of the key and Vue will re-render the component.

How do I set the timer on my Vue?

Vue. component("Timer", { template: "#timer", props: { deadline: { type: String, required: true }, speed: { type: Number, default: 1000 } }, data() { return { currentTime: null }; }, mounted() { setTimeout(this. countdown, 1); }, methods: { countdown() { let t = Date.

How do I force reload page Vue?

You can force-reload components by adding :key="$route. fullPath". However, :key="$route. fullPath" only can force-reload the components of the different route but not the components of the same route.

Does VUEX keep state on refresh?

To persist Vuex state on page refresh, we can use the vuex-persistedstate package. import { Store } from "vuex"; import createPersistedState from "vuex-persistedstate"; import * as Cookies from "js-cookie"; const store = new Store({ // ...


1 Answers

No need to re-invent the wheel, window.setInterval() does the job pretty well

Vue.component('events', {     template: '#events-template',      data () {         return {             list: [],             timer: ''         }     },     created () {         this.fetchEventsList();         this.timer = setInterval(this.fetchEventsList, 300000);     },     methods: {         fetchEventsList () {             this.$http.get('events', (events) => {                 this.list = events;             }).bind(this);         },         cancelAutoUpdate () {             clearInterval(this.timer);         }     },     beforeDestroy () {       this.cancelAutoUpdate();     } });  new Vue({     el: 'body', }); 
like image 194
Linus Borg Avatar answered Sep 20 '22 04:09

Linus Borg