Call method form setInterval() causing exception Call method form setInterval() causing exception typescript typescript

Call method form setInterval() causing exception


setInterval(this.runningLoop(this.element), 500);

The above invokes this.runningLoop before passing it to setInterval, setInterval is expecting a function but is receiving undefined. Wrap the call in an arrow function...

setInterval(() => this.runningLoop(this.element), 500);

And since you're not using the element argument in runningLoop, you can remove the argument and pass the method to setInterval...

setInterval(this.runningLoop, 500);


This will be looping in the time that you want:setInterval(() => function(), time in miliseconds);

Now, if you want to stop whenever! Write a var before the interval:Declare on the top of your TS file:varInterval: any;this.varInterval = setInterval(() => function(), time in miliseconds);

You can call the method to stop like this:clearInterval(this.varInterval);

This work for me!