Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JustInCase's avatar

Stopping a Action in Vuex

Hello!

this.$store.dispatch("TIMER", true);

this a action , it has

const time = setInterval(() => { context.commit('reduceTimer';) }, 1000;)

the action is called when mounted , but when i go to another page its still running! , is there any way to stop this?

and destory the const time

0 likes
3 replies
MohamedTammam's avatar

You can use clearInterval

In your store you add a property to store the interval.

let store  = {interval: null},

Store the interval when calling it.

TIMER(){
		store.x = setInterval(() => { context.commit('reduceTimer';) }, 1000;)
}

Then you create another action to clear it

actionName() {
	clearInterval(store.interval);
}

Then call the action that clear it on every route change.

new Vue({
  // ...
  watch: {
    '$route': function(to, from) {
     	store.dispatch('actionName');
    }
  }
})

PS: I didn't use mutations just for simplicity.

Please or to participate in this conversation.