Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 移动到其他页面时停止设置超时_Javascript_Vue.js - Fatal编程技术网

Javascript 移动到其他页面时停止设置超时

Javascript 移动到其他页面时停止设置超时,javascript,vue.js,Javascript,Vue.js,根据下面的代码,我的计时器工作正常,现在我想在用户移动到setTimeout之间的其他页面时停止计时器。提前谢谢 TimerCount.vue(可重用组件) 您将在{{倒计时}秒内被转发到服务页面 导出默认值{ 数据(){ 返回{ 倒数:10 } }, 方法:{ 倒计时{ 如果(this.countDown>=0){ 设置间隔(()=>{ 这是倒计时-=1 }, 1000) } } }, } parent.vue 方法:{ submitForm(){ formService.hospital

根据下面的代码,我的计时器工作正常,现在我想在用户移动到setTimeout之间的其他页面时停止计时器。提前谢谢

TimerCount.vue(可重用组件)


您将在{{倒计时}秒内被转发到服务页面
导出默认值{
数据(){
返回{
倒数:10
}
},
方法:{
倒计时{
如果(this.countDown>=0){
设置间隔(()=>{
这是倒计时-=1
}, 1000)
}
}
},
}
parent.vue

方法:{
submitForm(){
formService.hospital({
名字:这个,名字,
LastName:this.LastName,
description:this.description
})。然后(响应=>{
答复.数据;
反应;
this.issucessmessage=true;
this.isErrorMessage=false;
这是.store.dispatch('addPickupAssistanceMessage');
setTimeout(()=>this.$router.push({name:'services'}),10000);
}).catch(错误=>{
this.isErrorMessage=true;
此.$store.dispatch('addError'))
返回(this.errorMessage=JSON.stringify(
error.response.data.errorMessage
))
});
accesTimercount(){
这是。$refs.child.countDownTimer()
},
},

医院援助
{{message.FirstName}

如下所示,将
setTimeout
分配给一个变量,然后在
销毁前检查并清除它
hook:

methods: {
  submitForm() {
    // other code
    this.redirectTimeout = setTimeout(() => this.$router.push({ name: 'services'}),10000);
    // other code
  }
},
beforeDestroy() {
  if (this.redirectTimeout) {
    clearTimeout(this.redirectTimeout);
  }
}

一般的想法是,在销毁之前清除组件的
hook中的超时/间隔我已经尝试过了,但不知道应该从哪里调用clearTimeout..我在方法内部进行了尝试谢谢你的Psidom.它对我有效。我必须在多个组件中使用此方法相同的组件有没有常见的方法所有组件中的所有组件。