I am trying to use a setTimeout function that calls itself and is broken by a counter. However, I keep gettng a NaN error. Can someone help me?
<script language="javascript">
function Tests() {
this.i = 0;
}
Tests.prototype.increment_test = function () {
if (this.i > 2) {
return;
}
alert(this.i);
this.i++;
setTimeout(this.increment_test, 30);
}
ta = new Tests();
ta.increment_test();
</script>
When running a function from setInterval the context in “this” variable is not your object but “window” object. You need to pass the context in the following way: