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()的用法, 该文分析得比较详细。