0

I am currently working on a website similar to www.romneymakes.com. I'd like to create a function which simulates the red banner to the right of the website. I want to program a counter which increments every second.

I tried to work the programming out in a blank web page, but the code won't work. I'd like to know if someone can help me. The code I am using is pasted below.

function counter() {
    var per_sec = c.per_second, num_of_sec = 0, total;
    per_sec += 0.74;
    num_of_sec++;

    var seconds = Math.floor(num_of_sec / 10);
    total = per_sec * seconds;

    window.alert( total );
  }


  setInterval('counter', 1000);

2 Answers 2

1

If you pass a string to setInterval, it will be evaluated. In that case, you'd have to pass 'counter()' to call the function. But it's preferable to pass a function, rather than passing a string, so in your scenario you could simply write:

setInterval(counter, 1000);

You're also defining num_of_sec and total within the function, meaning they will only live for the duration of that function call. num_of_sec will be set to 0 every time the counter is called. Define these as global variables.

You're also setting seconds = Math.floor(num_of_sec / 10) which means that seconds (and hence anything you multiply by it) will be 0 for the first 10 seconds. Is this intentional? If you want your counter to increase only once every 10 seconds, instead of every second, you might be better off passing 10000 rather than 1000 as the interval delay.

Sign up to request clarification or add additional context in comments.

2 Comments

I intended for the counter to increase 0.74 every one second. Does this mean that the "seconds=math.floor()" line isn't necessary and I can get rid of it?
@JaPerk14: yes, in that case, that line of code doesn't serve your purposes at all
0

Change to:

setInterval(counter, 1000);

Also, c is not defined (something that you planned to pass as an argument to the function perhaps?):

c.per_second

Comments

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.