Skip to content

Instantly share code, notes, and snippets.

@themonster2015
Last active April 27, 2019 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themonster2015/2be04ce0f13e9bb5477c9998444d4a03 to your computer and use it in GitHub Desktop.
Save themonster2015/2be04ce0f13e9bb5477c9998444d4a03 to your computer and use it in GitHub Desktop.
vue code for periodically polling an api
new Vue({
el: '#app',
data: {
items: [],
interval: null,
},
methods: {
loadData: function () {
$.get('/api/data', function (response) {
this.items = response.items;
}.bind(this));
}
},
ready: function () {
this.loadData();
this.interval = setInterval(function () {
this.loadData();
}.bind(this), 30000);
},
beforeDestroy: function(){
clearInterval(this.interval);
}
});
<div id="app">
<div v-for="item in items">{{ item.prop }}</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment