0

I have been searching the entire day for a solution to this problem. I like this Counter I found on StackOverflow, but because I am inexperienced using JavaScript, I am not entirely sure how to stop it.

I figured out how to set the value and when to start counting etc, but now I want to add a maximum value. IE: If the counter reaches 20 million, then stop.

I tried the following, but it doesn't work:

var simplicity = formatMoney(20000000);

if (amount.innerText <= simplicity){
        function update() {
            var current = (new Date().getTime() - start)/1000*0.36+0;
            amount.innerText = formatMoney(current);
        }
    setInterval(update,1000);
}
else{
    amount.innerText = simplicity;
}
Community
  • 1
  • 1
Fizor
  • 1,480
  • 1
  • 16
  • 31
  • You are only checking your innerHTML against simplicity before your loop. You need to move that check inside your `update` function I think. – putvande Dec 10 '13 at 13:39

1 Answers1

1

Try this

var max = 20000000;

var intervalId = setInterval(function () {
    var current = (new Date().getTime() - start)/1000*0.36+0;
    if (current > max) {
        amount.innerText = formatMoney(max);
        clearInterval(intervalId);
    } else {
        amount.innerText = formatMoney(current);
    }
}, 1000);

Use clearInterval(id) to stop intervals.

Ben Barkay
  • 5,473
  • 2
  • 20
  • 29
  • Updated my answer. This code should be working given `start`, `formatMoney` and `amount` are defined and valid. – Ben Barkay Dec 10 '13 at 13:53