akmur
akmur

Reputation: 1635

Simple Javascript timed counter

I am doing the following:

function countUp(c){
  c.value = c.value + 1;
  $('#number').text(c.value);
}

var counter = {value: 0};

window.setTimeout(countUp(counter), 100);

For some reason the value of my counter seems to always stay 1. I tried using an object instead of a variable because it was my understanding that objects are passed by reference, not value, and I hoped I could change this value through my countUp function.

It's clearly not working, what's wrong?

Here is a Codepen

Upvotes: 0

Views: 83

Answers (1)

Mike
Mike

Reputation: 4091

You want setInterval. setTimeout is a one-shot deal.

setInterval(function () {
  countUp(counter)
}, 100);

Upvotes: 5

Related Questions