setInterval行应如下所示:
this.intervalID = setInterval( (function(self) { //Self-executing func which takes 'this' as self return function() { //Return a function in the context of 'self' self.retrieve_rate(); //Thing you wanted to run as non-window 'this' } })(this), this.INTERVAL //normal interval, 'this' scope not impacted here. );编辑 :相同的原则适用于“
onload”。在这种情况下,“外部”代码几乎不做任何事情,它只是建立一个请求,然后发送它。在这种情况下,不需要上面代码中的额外功能。您的retrieve_rate应该看起来像这样:-
retrieve_rate : function(){ var self = this; var ajax = new XMLHttpRequest(); ajax.open('GET', 'http://xyz.com', true); ajax.onreadystatechanged= function() { if (ajax.readyState == 4 && ajax.status == 200) { // prefs available as self.prefs } } ajax.send(null);}


