I want to use a simple countdown timer starting at 30 seconds from when the function is run and ending at 0. No milliseconds. How can it be coded?
相关问题
- How to fix IE ClearType + jQuery opacity problem i
- jQuery add and remove delay
- Include empty value fields in jQuery .serialize()
- Disable Browser onUnload on certain links?
- how to get selected text from iframe with javascri
You can do as follows with pure JS. You just need to provide the function with the number of seconds and it will do the rest.
To make the code for the timer appear in a paragraph (or anywhere else on the page), just put the line:
where you want the seconds to appear. Then insert the following line in your
timer()
function, so it looks like this:Expanding upon the accepted answer, your machine going to sleep, etc. may delay the timer from working. You can get a true time, at the cost of a little processing. This will give a true time left.
So far the answers seem to rely on code being run instantly. If you set a timer for 1000ms, it will actually be around 1008 instead.
Here is how you should do it:
To use, call:
I wrote this script some time ago:
Usage: