countdown.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. var COUNTDOWN = {
  2. mustStop: false,
  3. init: function( minutes, seconds ) {
  4. COUNTDOWN.minutes = minutes;
  5. COUNTDOWN.seconds = seconds;
  6. COUNTDOWN.mustStop = false;
  7. // $( "#countdown" ).width( ( minutes > 0 ? 90 : 0 ) + 90);
  8. // $( "#countdown" ).css( "background-color", "black" );
  9. },
  10. start: function() {
  11. if( COUNTDOWN.mustStop )
  12. return;
  13. COUNTDOWN.running = true;
  14. var currentMinutes = "";
  15. var currentSeconds = "";
  16. var imageMinutes = "";
  17. var imageSeconds = "";
  18. currentMinutes = COUNTDOWN.minutes;
  19. currentSeconds = COUNTDOWN.seconds;
  20. var nextMinutes = COUNTDOWN.minutes;
  21. var nextSeconds = COUNTDOWN.seconds - 1;
  22. if( nextSeconds < 0 && nextMinutes > 0 ) {
  23. nextSeconds = 59;
  24. nextMinutes = Math.min(0, nextMinutes -1);
  25. }
  26. COUNTDOWN.minutes = nextMinutes;
  27. COUNTDOWN.seconds = nextSeconds;
  28. if( currentMinutes <= 0 && currentSeconds < 10 )
  29. $( "#countdown" ).css( "background-color", "red" );
  30. if(parseInt(currentMinutes) < 10 ) currentMinutes = "0" + currentMinutes;
  31. if(parseInt(currentSeconds) < 10 ) currentSeconds = "0" + currentSeconds;
  32. for(i = 0; i < String(currentMinutes).length; i++) {
  33. imageMinutes += "<div class='clock n"+ String(currentMinutes)[i]+"'></div>";
  34. }
  35. for(i = 0; i < String(currentSeconds).length; i++) {
  36. imageSeconds += "<div class='clock n"+ String(currentSeconds)[i]+"'></div>";
  37. }
  38. if( COUNTDOWN.minutes > 0) {
  39. $("#subMinutes").empty().removeClass( "hide" ).append( imageMinutes );;
  40. $(".clock.clock.separator").removeClass( "hide" );
  41. } else {
  42. $("#subMinutes").empty().addClass( "hide" );
  43. $(".clock.clock.separator").addClass( "hide" );
  44. }
  45. $("#subSeconds").empty().append( imageSeconds );
  46. if( nextMinutes >= 0 && nextSeconds >= 0 )
  47. setTimeout( "COUNTDOWN.start()", 1000 );
  48. else
  49. COUNTDOWN.callback();
  50. },
  51. add: function(seconds) {
  52. console.log( "Adding " + seconds + " seconds to countdown" );
  53. COUNTDOWN.seconds = COUNTDOWN.seconds + seconds;
  54. },
  55. setTime: function( seconds ) {
  56. console.log( "Setting " + seconds + " seconds to countdown" );
  57. COUNTDOWN.seconds = seconds;
  58. },
  59. stop: function() {
  60. COUNTDOWN.mustStop = true;
  61. },
  62. callback: function() {
  63. console.log( "COUNTDOWN.callback" );
  64. }
  65. };