countDown Class in Javascript


/ Published in: JavaScript
Save to your folder(s)

A simple countDown class in Javascript:


Copy this code and paste it in your HTML
  1. /**
  2.  * CountDown Class
  3.  *
  4.  * @author Giovambattista Fazioli
  5.  * @email g.fazioli@undolog.com
  6.  * @web http://www.undolog.com
  7.  *
  8.  * @param dd (string) 'month day, year'
  9.  *
  10.  */
  11. function countDown( dd ) {
  12. // init target time
  13. var target = new Date( dd );
  14. this.targetTime = target.getTime();
  15. /**
  16. * refresh countdown
  17. */
  18. this.refresh = function() {
  19. var today = new Date();
  20. var currentTime = today.getTime();
  21. // time left
  22. this._leftMilliseconds = (this.targetTime - currentTime);
  23. this._leftSeconds = Math.floor( this._leftMilliseconds / 1000 );
  24. this._leftMinutes = Math.floor( this._leftSeconds / 60 );
  25. this._leftHours = Math.floor( this._leftMinutes / 60 );
  26. // no module
  27. this.leftDays = Math.floor( this._leftHours / 24 );
  28. // for print
  29. this.leftMilliseconds = this._leftMilliseconds % 1000;
  30. this.leftSeconds = this._leftSeconds % 60;
  31. this.leftMinutes = this._leftMinutes % 60;
  32. this.leftHours = this._leftHours % 24;
  33. }
  34. this.refresh();
  35. }

URL: http://www.undolog.com/2008/10/13/una-classe-countdown-in-javascript/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.