`
muyu
  • 浏览: 218577 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

setTimeout 如何调用本身

阅读更多
MoviePlayer.prototype.moveDiv = function() {
 var curDiv = $('u' + this.groupName + '_' + this.current_id);
 if(curDiv.style.posLeft != 0) {
  curDiv.style.posLeft = curDiv.style.posLeft * 0.8;
  var self = this;
   window.setTimeout(function(){ self.moveDiv(); }, 30);
 } else {
  
 }
}

 

 

上述调用的时候,要注意两个地方:

 

  • 注意this引用的存储,如果在setTimeout里写this,那么系统会认为this指向setTimeout方法所属的window对象;
  • setTimeout里的参数的正确写法:window.setTimeout(function(){ self.moveDiv(); }, 30);而不是window.setTimeout(self.moveDiv, 30);

非常感谢js 中 setTimeout()的用法, 该文分析得比较详细。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics