PageRenderTime 67ms CodeModel.GetById 40ms RepoModel.GetById 1ms app.codeStats 0ms

/public/javascripts/countdown.js

https://github.com/fboulay/play-planning-center
JavaScript | 72 lines | 54 code | 12 blank | 6 comment | 5 complexity | 6585aa14f97d616d528493d8023f7914 MD5 | raw file
  1. /*
  2. * Source : http://stuntsnippets.com/javascript-countdown/
  3. */
  4. var jsCountdown = function () {
  5. var time_left = 10; //number of seconds for countdown
  6. var output_element_id = 'countdown_time';
  7. var keep_counting = 1;
  8. var no_time_left_message = 'Countdown over';
  9. function countdown() {
  10. if(time_left < 2) {
  11. keep_counting = 0;
  12. }
  13. time_left = time_left - 1;
  14. }
  15. function add_leading_zero(n) {
  16. if(n.toString().length < 2) {
  17. return '0' + n;
  18. } else {
  19. return n;
  20. }
  21. }
  22. function format_output() {
  23. var hours, minutes, seconds;
  24. seconds = time_left % 60;
  25. minutes = Math.floor(time_left / 60) % 60;
  26. hours = Math.floor(time_left / 3600);
  27. seconds = add_leading_zero( seconds );
  28. minutes = add_leading_zero( minutes );
  29. hours = add_leading_zero( hours );
  30. return hours + ':' + minutes + ':' + seconds;
  31. }
  32. function show_time_left() {
  33. document.getElementById(output_element_id).innerHTML = format_output();//time_left;
  34. }
  35. function no_time_left() {
  36. //document.getElementById(output_element_id).innerHTML = no_time_left_message;
  37. window.location.reload();
  38. }
  39. return {
  40. count: function () {
  41. countdown();
  42. show_time_left();
  43. },
  44. timer: function () {
  45. jsCountdown.count();
  46. if(keep_counting) {
  47. setTimeout("jsCountdown.timer();", 1000);
  48. } else {
  49. no_time_left();
  50. }
  51. },
  52. init: function (time, element_id) {
  53. time_left = time;
  54. output_element_id = element_id;
  55. jsCountdown.timer();
  56. }
  57. };
  58. }();
  59. //time to countdown in seconds, and element ID
  60. //jsCountdown.init(3673, 'countdown_time');