Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. function fancyTimeFormat(duration) {
  2. const hrs = ~~(duration / 3600);
  3. const mins = ~~((duration % 3600) / 60);
  4. const secs = ~~duration % 60;
  5. // Output like "1:01" or "4:03:59" or "123:03:59"
  6. let ret = "";
  7. if (hrs > 0) {
  8. ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
  9. }
  10. ret += "" + mins + ":" + (secs < 10 ? "0" : "");
  11. ret += "" + secs;
  12. return ret;
  13. }
  14. function playDing() {
  15. const ding_sound = new Audio('ding-sound-effect_2.mp3');
  16. ding_sound.play();
  17. }
  18. function startTimerOnElement() {
  19. let currElement = $('#task_list > .current_element').first();
  20. if (!currElement.length) {
  21. $('#timer_reset_btn').prop('disabled', true);
  22. $('#timer_next_btn').prop('disabled', true);
  23. $('#timer_start_btn').prop('disabled', false);
  24. $('#spent_time').text('0:00');
  25. $('#rest_time').text('0:00');
  26. $('#element_title').text('');
  27. $('#task_list > .used_element').removeClass('used_element');
  28. return;
  29. }
  30. let element_ttl = currElement.data('ttl');
  31. let timer = element_ttl;
  32. $('#spent_time').text(fancyTimeFormat(element_ttl - timer));
  33. $('#rest_time').text(fancyTimeFormat(timer));
  34. let title = currElement.html();
  35. if (title) {
  36. let short_title = '';
  37. $($.parseHTML(title)).filter('b').each(function () {
  38. short_title += $(this).text() + ' ';
  39. });
  40. $('#element_title').text(short_title);
  41. }
  42. timerId = setInterval(function () {
  43. --timer;
  44. $('#spent_time').text(fancyTimeFormat(element_ttl - timer));
  45. $('#rest_time').text(fancyTimeFormat(timer));
  46. if (!timer) {
  47. clearInterval(timerId);
  48. timerId = null;
  49. //playDing();
  50. startNextTimer(currElement);
  51. }
  52. }, 1000);
  53. }
  54. function startNextTimer(currElement) {
  55. let nextElement = currElement.parent().children('li.current_element + li');
  56. if (!nextElement) {
  57. $('#spent_time').text('0:00');
  58. $('#rest_time').text('0:00');
  59. return;
  60. }
  61. currElement.removeClass('current_element').addClass('used_element');
  62. nextElement.addClass('current_element');
  63. startTimerOnElement();
  64. }
  65. function calcTotalTimeSeconds() {
  66. let total_time = 0;
  67. $('#task_list').children('li').each(function () {
  68. let ttl = $(this).data('ttl');
  69. if (!ttl || !parseInt(ttl) || ttl != parseInt(ttl)) {
  70. alert('Illegal element TTL value "' + ttl + '"!');
  71. return false;
  72. }
  73. total_time += ttl;
  74. });
  75. return total_time;
  76. }
  77. let timerId;
  78. $(document).ready(function () {
  79. $('#total_rest_time').text(fancyTimeFormat(calcTotalTimeSeconds()));
  80. $('#timer_start_btn').on('click', function () {
  81. if (timerId) {
  82. return;
  83. }
  84. $(this).prop('disabled', true);
  85. $('#timer_reset_btn').prop('disabled', false);
  86. $('#timer_next_btn').prop('disabled', false);
  87. //playDing();
  88. if (!$('#task_list > .current_element').length) {
  89. $('#task_list > li').first().addClass('current_element');
  90. }
  91. startTimerOnElement();
  92. })
  93. $('#timer_reset_btn').on('click', function () {
  94. if (!timerId) {
  95. return;
  96. }
  97. clearInterval(timerId);
  98. timerId = null;
  99. startTimerOnElement();
  100. });
  101. $('#timer_next_btn').on('click', function () {
  102. if (!timerId) {
  103. return;
  104. }
  105. clearInterval(timerId);
  106. timerId = null;
  107. let currElement = $('#task_list > .current_element').first();
  108. if (!currElement) {
  109. $('#spent_time').text('0:00');
  110. $('#rest_time').text('0:00');
  111. return;
  112. }
  113. startNextTimer(currElement);
  114. });
  115. });