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/)