Morgan Wilde
Morgan Wilde

Reputation: 17303

setInterval with an object method only running once

The following structure

function SomeObject() {}

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

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

Produces only a single "what?" in the console, why isn't it just running perpetually?

I call it like so

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

Upvotes: 0

Views: 46

Answers (1)

Hugo Tunius
Hugo Tunius

Reputation: 2879

You aren't calling repeatWhat in your code

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

Upvotes: 2

Related Questions