I'm trying to use "setInterval" and "clearInterval" functions inside js objects created with "new" operator.
html:
<button onclick="testList.push(new Test());">run</button>
js:
Test = function(){
this.y = 0;
this.dy = 10;
that = this;
this.interval = setInterval(function(){
that.y += that.dy;
if (that.y>300){
console.log(that.y); //prints y in console
clearInterval(that.interval);
}
}, 10);
}
When I click the "run" button once this works as I expect. But when I click the "run" button multiple times before a previous interval is not cleared (when click the button multiple times quickly), then intervals are not cleared and y is incremented infinitely. What is the mistake I'm making here?
Thank you in advance...