We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

gfazioli on 10/13/08


Tagged

javascript class countDown


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

gfazioli
SpinZ


countDown Class in Javascript


Published in: JavaScript 


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

A simple countDown class in Javascript:

  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. }

Report this snippet 

You need to login to post a comment.