new Something({
events:{
load: (function loopsiloop(){
console.log(this); // firstime this will be Something, but next call what its
setTimeout(function(){
console.log(this);
$.ajax({
url: 'foo.htm',
context: this,
success: function( response ){
// do something with the response
},
error: function(){
// do some error handling. you
// should probably adjust the timeout
// here.
},
complete: function(){
loopsiloop(); // recurse //need to bind this here like loopsiloop().bind(this)
}
});
}.bind(this), 5000);
}),
click: function(){
alert("clicked");
}
}
})
请仔细阅读代码并阅读注释,这里的问题是我需要this
在setTimeOut
函数中使用,所以我绑定this
到setTimeOut
,但是当我以递归方式调用函数时,值this
将不一样
注意:-我不想将对象传递给函数并且不想使用setIntervel
(http://www.erichynds.com/javascript/a-recursive-settimeout-pattern/)