Infinite loop possibility
See original GitHub issueIf you follow one of the examples given in the readme, you may end up in an infinite loop.
var endVal = 9645.72;
var numAnim = new CountUp("targetElem", 0, endVal - 100, 1.5);
numAnim.start(function() {
// Try adding this log message to see that it is being printed repeatedly
console.log(endVal + ' ' + numAnim.frameVal + ' ' + (endVal === numAnim.frameVal));
numAnim.update(endVal);
});
The infinite loop is: callback() > self.update() > self.count() > callback() > self.update() > self.count() > …
The reason seems to be a precision error when comparing numbers in update() function:
if (newEndVal === self.frameVal) return;
One workaround is to clear the callback before calling update, so it won’t be called again on the next iteration:
numAnim.start(function() {
numAnim.callback = null;
numAnim.update(endVal);
});
Another workaround is to compare numbers in the update function with some tolerance:
if (Math.abs(newEndVal - self.frameVal) < epsilon) return;
i.e. consider numbers being equal if their difference is less than some value epsilon
. It’s hard to say what value to use for epsilon
, because it may be different for different use cases. In case of the example above epsilon
of 0.1 seems to be enough.
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Infinite loop - Wikipedia
In computer programming, an infinite loop (or endless loop) is a sequence of instructions that, as written, will continue endlessly, unless an external ......
Read more >Probability brain teaser with infinite loop - Math Stack Exchange
I found this problem and I've been stuck on how to solve it. A miner is trapped in a mine containing 3 doors....
Read more >8 common causes of infinite C# loops (with examples) · Kodify
An infinite loop makes our C# program stuck, and can even cause an application crash. Luckily, infinite loops are often easy to solve....
Read more >What is Infinite Loop?. Infinite loop also called endless loop…
The infinite loop also called endless loop is a type of loop that never ends. Instructions inside the loop will execute endlessly. The...
Read more >Infinite Loop | Code Snippets Wiki - Fandom
Apparently, there is no possibility for an infinite loop in the server, but if there are two such servers (A and B), and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think your right. Probably the best thing is to use
Number()
and then do a check withensureNumber()
like we do with startVal and endVal.I have submitted a pull request with the proposed changes.