---
title: "setTimeout 如何调用本身"
author: "muyu"
date: "2008-10-16T14:09:37Z"
tags: []
slug: "settimeout"
draft: false
lastmod: "2008-10-16T14:09:37Z"
id: 253575
url: "https://muyu.iteye.com/blog/253575"
---

```
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()的用法](http://www.cnblogs.com/zhangzheny/archive/2007/12/03/981200.html), 该文分析得比较详细。