setInterval,对象方法只运行一次

时间:2013-12-18 20:19:27

标签: javascript setinterval

以下结构

function SomeObject() {}

SomeObject.prototype.repeatWhat = function() {
    setInterval(this.what, 1);
}

SomeObject.prototype.what = function() {
    console.log("what?");
}

在控制台中只生成一个"what?",为什么不能永久运行?

我称之为

var someInstance = new SomeObject();
someInstance.what();

1 个答案:

答案 0 :(得分:2)

您没有在代码中调用repeatWhat

var someInstance = new SomeObject();
someInstance.what();
someInstance.repeatWhat(); //I've added this