question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Infinite loop possibility

See original GitHub issue

If 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:closed
  • Created 6 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
inorganikcommented, May 8, 2017

I think your right. Probably the best thing is to use Number() and then do a check with ensureNumber() like we do with startVal and endVal.

0reactions
yesenarmancommented, May 8, 2017

I have submitted a pull request with the proposed changes.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found