0

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

2
  • Is there a reason you are making counter a class with a value property, instead of just as a normal var: ie - var counter = 0; Commented Mar 3, 2015 at 15:11
  • I might be wrong, but if I simply a variable it seems to not be working. With an object, I can change the object value within my countUp function. I am learning (forever noob) so I might be wrong... Check it in the codepen link Commented Mar 3, 2015 at 15:48

1 Answer 1

5

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

setInterval(function () {
  countUp(counter)
}, 100);
Sign up to request clarification or add additional context in comments.

2 Comments

Put your function call into an actual function as well. Edited my post.
Super, thanks!, I'll choose your answer in 10 minutes :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.