html 取消settimeout,vue中setTimeout无法通过clearTimeout清除问题

f1dd291eff26a3eeb2efe4e0acddf1a1.png

在异步清除中,利用vue 中data存放setTimeout的标识进行清除时,无法清除。则需要在函数前加上window.即可

如window.setTimeout与window.clearTimeout

具体代码如下

精简后的代码。

环境为electron-vue 渲染进程异步获取主进程上html并渲染到页面、过程中需要有loading的显示。

setTimeout 与clearTimeout 未加window时,this.timeOutLoading事件总会被触发。

const {ipcRenderer:ipc} = require('electron');

export default {

data(){

return{

activeName: 'second',

html:'',

loading:false,

timeOutLoading:0

}

},

methods:{

handleClick(tab, event) {

if(tab.name == 'first' && this.loading == false){

if(this.timeOutLoading != 0){

window.clearTimeout(this.timeOutLoading);

}

this.html = "

加载中...
";

this.loading = true;

this.timeOutLoading = window.setTimeout(() => {

if(this.loading == true){

this.loading = false;

this.html = "

加载超时
";

}

}, 3000);

window.setTimeout(() => {

ipc.send("getPage");

}, 500);

}

}

},

mounted(){

ipc.on('getPage-reply', (event, arg) => {

if(this.timeOutLoading != 0){

window.clearTimeout(this.timeOutLoading);

this.timeOutLoading = 0;

}

this.loading = false;

this.html = arg;

});

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值