CountDown a class in Javascript
Monday, October 13, 2008In the post 3D Countdown with FIVe3D (also see How I Did It: Write a countdown in Flash), was given a class for creating an object CountDown in Actionscript, here is a similar version in Javascript:
- / **
- * Class CountDown
- *
- * @ Author Giovambattista Fazioli
- * @ Email @ g.fazioli undolog.com
- * @ Web http://www.undolog.com
- *
- Dd * @ param (string) 'month day, year'
- *
- * /
- dd ) { function countdown (dd) (
- / / Init target time
- new Date ( dd ) ; var target = new Date (dd);
- = target. getTime ( ) ; this. targetTime = target. getTime ();
- / **
- * Refresh countdown
- * /
- = function ( ) { this. refresh = function () (
- new Date ( ) ; var today = new Date ();
- today. getTime ( ) ; var currentTime = today. getTime ();
- / / Time left
- ( this . targetTime - currentTime ) ; this. _leftMilliseconds = (this. targetTime - currentTime);
- Math. floor ( this ._leftMilliseconds / 1000 ) ; this. _leftSeconds = Math. floor (this. _leftMilliseconds / 1000);
- Math. floor ( this ._leftSeconds / 60 ) ; this. _leftMinutes = Math. floor (this. _leftSeconds / 60);
- Math. floor ( this ._leftMinutes / 60 ) ; this. _leftHours = Math. floor (this. _leftMinutes / 60);
- / / No module
- = Math. floor ( this ._leftHours / 24 ) ; this. leftDays = Math. floor (this. _leftHours / 24);
- / / For print
- = this ._leftMilliseconds % 1000 ; this. leftMilliseconds = this. _leftMilliseconds% 1000;
- = this ._leftSeconds % 60 ; this. leftSeconds = this. _leftSeconds% 60;
- = this ._leftMinutes % 60 ; this. leftMinutes = this. _leftMinutes% 60;
- = this ._leftHours % 24 ; this. leftHours = this. _leftHours% 24;
- )
- ( ) ; this. refresh ();
- )
Example
- new countDown ( '1 1, 2009' ) ; var cd = new countdown ('1 1, 2009 ');
- / / Show how many days, hours, minutes, seconds and milliseconds to January 2009
- cd. leftDays + "," + cd. leftHours + "," + cd. leftMinutes + "," + cd. leftSeconds + "," + cd. leftMilliseconds ) ; document. write (cd. leftDays + "" + cd. leftHours + "" + cd. leftMinutes + "" + cd. leftSeconds + "" + cd. leftMilliseconds);














Hello Giovambattista,
I find very interesting this JavaScript solution, because it is simple. As a lover of scripting, I've made some changes to your code that you place the following:
Basically adding more structure and represented by the output method, which allows you to print the countdown with the desired format by the user (in a very similar way as happens in PHP).
For example, if you wanted to print the date, hours, minutes, seconds and milliseconds missing through our countdown, we could proceed as follows, each time with no recourse to appeal the internal properties of our class:
The method "render" finally takes care of the "normalize" the format of numbers.
Another interesting addition would be to a method "start", which calls the output periodically, so you look like a real countdown (countdown that can be stopped by their method "stop").
Richard Worthy
@ Richard Worthy: really a nice variant, bravo! Especially interesting technique to format the output: ingenious and subtle! In the method
output()would put a limit toreturn(output)rather than adocument.write()... but it is a detail. A timer withstart()andstop()make it all really complete. Congratulations and thanks for the interesting suggestions@ Giovambattista Fazioli:
Certainly, I entered the
document.write()for a prompt notification of the test, in the final version of production and areturnis a mustAnything, I was pleased that exchange of technical advice
Soon,
Richard Worthy
Update: Microsoft Internet Explorer 7, the date in numeric format, type
"3 5, 2008"produces error. To delete just enter the "month" using 3 letters in English: Jan, Feb, Mar, etc ...@ Giovambattista Fazioli:
Hello everyone.
I found this very interesting script.
My only problem? Being an idiot with javascript find it difficult to insert into a web page.
The change that I made the code has been put "return" instead of document.write, but I can not recall then the page id not understanding what I use.
Can you help?
thanks
hello