码迷,mamicode.com
首页 > 其他好文 > 详细

vue中使用setTimeout

时间:2017-06-05 12:35:42      阅读:903      评论:0      收藏:0      [点我收藏+]

标签:bsp   span   elf   class   set   log   color   time   逻辑   

在vue的函数中使用setTimeout

self.distroyTimeout = setTimeout(()=>{

  self.initData()

},1000)

 

这时清除setTimeout需要在destoryed周期中进行

destoryed(){

  window.clearTimeout(this.distroyTimeout)

}

 

但是这样做在逻辑复杂的情况下还是出现了没有关闭setTimeout的情况,以下是一种更好的解决方案

let self = this

if(self && self.distroyTimeout){

  setTimeout(()=>{

    if(self && self.distroyTimeout){

      self.initData()
    }
  },1000)

}

 

在这里distroyTimeout属性表示当前这个组件是否被销毁

vue中使用setTimeout

标签:bsp   span   elf   class   set   log   color   time   逻辑   

原文地址:http://www.cnblogs.com/cnundefined/p/vue_setTimeout.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!