我想知道为什么我不能在第14行使用setTimeout(this.updateState, 1000);
。当我使用setTimeout(v.updateState, 1000)
时,代码可以正常工作。
var GREEN = 1, YELLO= 2, RED = 3;
var v = {
state: GREEN,
updateState: function(){
if(this.state == GREEN)
this.state = YELLO;
else if(this.state == RED)
this.state = GREEN;
else
this.state = RED;
console.log(this.state);
setTimeout(this.updateState, 1000); // this line works when I have setTimeout(v.updateState, 1000); wondering why this.updateState does not work
}
};
setTimeout(v.updateState, 1000);