Slightly Off-Topic: Standard JavaScript timers are horribly inaccurate – see the very left example here. The timer on the right is much more accurate, an explanation can be found here.

01-20-2013, 04:12 AM

muldaria

Im an intern for a website startup and my "boss" asked me to make a time that goes to 60 seconds infinitely. so can you somehow show me the code for that?

01-20-2013, 04:18 AM

Logic Ali

If by "loop" you mean execute each iteration of a loop at delayed intervals, then that isn't possible in JavaScript, nor should it be.

That's why setTimeout and setInterval exist and your type of implementation must be used.

Of course it could be improved, not least by ensuring that the function ceases to be called after it is no longer required.

01-20-2013, 05:21 AM

Airblader

Going completely off-topic now:

When I started as an intern, the first thing they gave me was the book "Clean Code" – and at that point I already had ten years of programming experience. If you're going for making this your profession you really wanna read similar books ... even if you have to do it in your free time. The sooner you learn how to write quality code the easier it will be to take it in.

As for your task: What exactly is your task? A timer that goes from 0 to 60 or from 60 to 0? Does it have to have tenths of seconds? Should it just repeat itself when it reached the end?

01-20-2013, 07:18 AM

Old Pedant

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">: