In my SPA made with VUE I have a component running a recursive few of setInterval functions (it's a countdown). I noticed the countdown continue in the background when I switch view to another component, but I prefer to destroy the setInterval.
I tried using a global data having the countdowns and then destroy it on destroyed hook but it doesn't work.
Here my code:
data: function () {
return {
counters: ""
}
}),
methods: {
countdown(index, exp) {
...
this.counters = setInterva()
...
},
},
destroyed(){
console.log(this.counters); // returns a progressive integer
clearInterval(this.counters);
console.log(this.counters); // returns same integer
this.counters = 0;
console.log("destroyed");
}
But in the console I got:
destroyed
app.js:64433 0
app.js:64398 Missing counter_1 . <--- which means counter is still running
Thanks for any suggestion!
this
when you use it inside of the method might actually not refer to the vue instance (a context problem?) so that when you reference it in the destroyed hook it might not be assigned to that variable. Have you tried to console logthis.counters
in the destroyed hook? Is it defined? – Escobedo