You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timer.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. function fancyTimeFormat(duration) {
  2. // Hours, minutes and seconds
  3. const hrs = ~~(duration / 3600);
  4. const mins = ~~((duration % 3600) / 60);
  5. const secs = ~~duration % 60;
  6. // Output like "1:01" or "4:03:59" or "123:03:59"
  7. let ret = "";
  8. if (hrs > 0) {
  9. ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
  10. }
  11. ret += "" + mins + ":" + (secs < 10 ? "0" : "");
  12. ret += "" + secs;
  13. return ret;
  14. }
  15. function play_ding() {
  16. let ding_sound = new Audio('ding-sound-effect_2.mp3');
  17. ding_sound.play();
  18. }
  19. function startTimerOnElement() {
  20. let currElement = $('#task_list > .current_element').first();
  21. if (!currElement.length) {
  22. $('#timer_reset_btn').prop('disabled', true);
  23. $('#timer_start_btn').prop('disabled', false);
  24. $('#spent_time').text('0:00');
  25. $('#element_title').text('');
  26. return;
  27. }
  28. let element_ttl = currElement.data('ttl');
  29. let title = currElement.html();
  30. if (title) {
  31. let short_title = '';
  32. $($.parseHTML(title)).filter('b').each(function () {
  33. short_title += $(this).text() + ' ';
  34. });
  35. $('#element_title').text(short_title);
  36. }
  37. let timer = element_ttl;
  38. timerObj = setInterval(function () {
  39. $('#spent_time').text(fancyTimeFormat(element_ttl - timer));
  40. $('#rest_time').text(fancyTimeFormat(timer));
  41. if (!timer--) {
  42. clearInterval(timerObj);
  43. //play_ding();
  44. timer = 0;
  45. let nextElement = currElement.parent().children('li.current_element + li');
  46. if (nextElement) {
  47. currElement.removeClass('current_element');
  48. nextElement.addClass('current_element');
  49. startTimerOnElement();
  50. }
  51. }
  52. }, 1000);
  53. }
  54. let timerObj;
  55. $(document).ready(function () {
  56. let total_time = 0;
  57. $('#task_list').children('li').each(function () {
  58. let timestr = fancyTimeFormat($(this).data('ttl'));
  59. total_time += $(this).data('ttl');
  60. /*
  61. $(this).on('click', function () {
  62. $(this).parent().children('.current_element').removeClass('current_element');
  63. $(this).addClass('current_element');
  64. });
  65. */
  66. });
  67. $('#total_rest_time').text(fancyTimeFormat(total_time));
  68. $('#timer_start_btn').on('click', function () {
  69. $(this).prop('disabled', true);
  70. $('#timer_reset_btn').prop('disabled', false);
  71. //play_ding();
  72. if (!$('#task_list > .current_element').length) {
  73. $('#task_list > li').first().addClass('current_element');
  74. }
  75. startTimerOnElement();
  76. })
  77. $('#timer_reset_btn').on('click', function () {
  78. if (!timerObj) {
  79. return;
  80. }
  81. clearInterval(timerObj);
  82. startTimerOnElement();
  83. });
  84. });