I have the following code that should display an incrementing number once a second:
let timer_demo = {
count: 0,
timer: null,
update: function() {
this.count++;
console.log(this.count);
this.timer = setTimeout(this.update, 1000);
}
};
timer_demo.update();
However, when I run this in Chrome I get a "1", then a "NaN" a second later and then nothing. The timer stops. I get the feeling the problem is that I'm not understanding what is going on with "this" in this context. I just don't know what it is. The fact that the second time the "update()" method gets invoked, the "count" field is "NaN" would seem to support this assertion. Can anybody shed some light on this for me?
Thanks!