I am trying to call a method again from within itself after a period of time, but it isn't working.
I have the the following:
Hi.prototype.stuff = function(){
console.log("hello")
setTimeout(()=>this.stuff(), 1000)
}
However, it outputs "hello" to the console only once. It should output "hello" twice. Is there a way to do this?
Did you declare and instanciate Hi
correctly? This seems to work:
function Hi(name) {
this.name = name;
}
Hi.prototype.stuff = function() {
console.log(`Hello ${this.name}`);
setTimeout(() => this.stuff(), 1000);
};
const x = new Hi('Dshiz');
x.stuff();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments