View Single Post
Old 01-20-2013, 06:18 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
And a little on the side.

Keeping in mind Airblader's warning about timer accuracy, at least you can simplify the code.

There is no reason for separate variables for seconds and tenths-of-seconds.

Oh, and your use of document.counter.d2.value would indicate you are using a named <form>, which is considered obsolete. So assuming you have a <form> with, properly, an id such as <form id="counter">:
Code:
var ticker = 0;
var countField = document.getElementById("counter").d2;
function tick( )
{
    if ( (ticker-=0.1) <= 0 ) { ticker = 60; }
    countField.value = ticker.toFixed(1);
}
tick( );
setInterval( tick, 100 );
That counts DOWN from 60 to 0.1 before resetting to 60.

If you wanted to go UP, from 0 to 59.9 before resetting to 0:
Code:
var ticker = 60;
var countField = document.getElementById("counter").d2;
function tick( )
{
    if ( (ticker+=0.1) >= 60 ) { ticker = 0; }
    countField.value = ticker.toFixed(1);
}
tick( );
setInterval( tick, 100 );
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote