-2

I'm trying to have a simple html page that counts up indefinitely. But for some reason my second variable is not being displayed. The second variable is an embedded javascript timer As you can see the first variable is displayed, but not the second. Apologies, I'm a beginner.

https://dl.dropboxusercontent.com/u/44188718/time.html

<script>
var test;
test = 123;
</script>

<script>document.write(test)</script>


<script>
var time;
time = 0;

function startTimer()
{
    window.setTimeout(add(), 60 *  1000);
}

add()
{
    var += 1;
}
</script>

<script>document.write(time)</script>

3 Answers3

1

Your middle script element contains several errors.

This line:

window.setTimeout(add(), 60 *  1000);

should be:

window.setInterval(add, 60 * 1000);

setTimeout() calls a function exactly once. setInterval() calls a function repeatedly. Also note that I've removed the () after add - with parentheses it calls the function immediately and passes its result to setTimeout() or setInterval(), but you want to pass the function itself so just use the function name with no parentheses. (And the window. part is optional).

Then this line:

add()

should be:

function add()

And this:

    var += 1;

should be:

    time += 1;
    // OR
    test += 1;

(I'm not sure which variable you intend to increment.)

Also you never call the startTimer() function.

Finally, in your add() function you'd need to actually output the value. document.write() is almost never a good idea, so I'd suggest creating an element on your page to hold the number and updating its content:

<div id="timer"></div>

document.getElementById("timer").innerHTML = time; // or = test; (whichever var you want)

Demo of all of the above: http://jsfiddle.net/Y3XMk/

Or the same effect with simplified code: http://jsfiddle.net/Y3XMk/1/

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
1

There are several misconsceptions in your code, so I will try to go through them.

  1. You have to specify the function keyword when you define a function. In your case:

    add(){ ... }
    

    Should be:

    function add() { ... }
    

    Then you can safely call it from your setTimeout code.

  2. Using the keyword var you are basically telling that you are about to define a variable for the current scope. First of all, since the current scope is window anyways (the global scope), you don't need to add var. Secondly, you have to use the variable name when you change it, or add to it. In your case:

    var += 1
    

    Should be:

    time += 1

  3. Functions get called once, when you call them. In your case, document.write(time) will get called once when the document is parsed, and hence will write 1 in your document. You have to call the write method inside your add function, to have numbers show up at every interval. So:

    function add(){
        time += 1;
        document.write(time);
    }
    
  4. setTimeout calls the specified callback function once, after the time is passed, I believe you are looking for setInterval. You can use it like so: function startTimer(){ window.setInterval(add, 60 * 1000); } But don't forget to call your function, in order for the timer to start.

Here is an example of your code (it works every second instead):

Working fiddle

Community
  • 1
  • 1
Sunyatasattva
  • 5,619
  • 3
  • 27
  • 37
0

There are a lot of things wrong with your code.

I believe what you are trying to do is this:

<script>
    var time = 0;
    window.setInterval(function(){
    document.write(time);
    time++;
    }, 60*1000);
</script>

See a working demo here:

http://jsfiddle.net/8MVFB/

Just click "Run".

HaukurHaf
  • 13,522
  • 5
  • 44
  • 59