在对象中调用setInterval会覆盖“this”

时间:2018-02-08 20:14:44

标签: javascript

我正在编写代码来对api中的某些资源执行“监视”: 考虑我的代码:

function myObj(){
   this.endpoint  = "/example"; // my endpoint
   this.interval = 500; // my watch interval
}

myObj.prototype.watch = function(){
   console.log(this.endpoint); // returns "/example"
   this.intervalId = setInterval(this.refresh, this.interval);
}

myObj.prototype.destroy= function(){
   clearInterval(this.intervalId);
}

myObj.prototype.refresh = function(){

   console.log(this.endpoint) // -> undefined ??? 

}

0 个答案:

没有答案
相关问题