There are two JS functions for handling timed events:
setTimeout[
^] and
setInterval[
^]. The core difference between these two functions is that
setInterval
repeats, while
setTimeout
only happens once.
In your code you're starting this animation using
setInterval
, and then within the function that runs the animation you're starting another interval
every time:
var rotID = setInterval(rot, 1000)
You clear this down only when:
if ((eLeftPos + s) >= 1000) {
clearInterval(rotID)
console.log(rotID)
}
Which means that every 100ms you're starting another interval at 1 second, and any that don't get cleared immediately just get left to run without any ability to cancel them. You could consider using
setTimeout
instead within the
myInterval
method, which will ensure that you don't end up stacking up interval timers. But even so, it means you're creating new timeouts set to 1 second ahead, every 100ms. I don't think this is what you're intending with your code?