Update: After writing this I realized I was wrong about recursion and the setTimeout function. Thus this method really doesn’t have any flaws. Though for a more advanced timer please see my next post.
Please note that with all my sample scripts from this point on I require that my name and website be retained in the event of using the code. If you wish to use without my information please email me.
First the code for those that just want to use it:
And the code used to start the timer:
Notice how I wrote the function to be passed the field which I wish to modify. This easily allows for more robustness as I can easily change my timer function to update any other html entity with a value field. With a bit more trickery this function could take another argument which will allow it to modify the innerHTML field if the element specified by the id doesn’t have a value, however I’ll leave that up to you to figure out.
One flaw I noticed while testing this post is that each time the start button is clicked a new timer is started thus making the counter go much faster. Feel free to correct that if needed, but it shouldn’t be a problem if you’re sure the timer will only be started once.
I think that’s it, feel free to email me, or comment with any questions you may have regarding this.