counter.js (1099B)
1 function gettime() { 2 return Math.trunc(Date.now() / 1000) 3 } 4 5 function parseduration(s) { 6 let hr = s.match(/(\d+)\s*h/); 7 let mn = s.match(/(\d+)\s*m/); 8 let sc = s.match(/(\d+)\s*s/); 9 return (parseInt(hr[1]) * 3600) + (parseInt(mn[1]) * 60) + parseInt(sc[1]) 10 } 11 12 function sn(n) { 13 return String(n).padStart(2, "0") 14 } 15 16 function duration(duration) { 17 let mn = Math.trunc(duration / 60) 18 let sc = duration % 60 19 let hr = Math.trunc(mn / 60) 20 mn = mn % 60 21 22 return sn(hr) + ':' + sn(mn) + ':' + sn(sc) 23 } 24 25 function updatetime(counter, start, total, total_elapsed) { 26 let elapsed = gettime() - start 27 counter.textContent = duration(elapsed) 28 if (total) 29 total.textContent = duration(total_elapsed + elapsed) 30 /* if (start.checkVisibility()) */ 31 setTimeout(updatetime, 1000, counter, start, total, total_elapsed) 32 } 33 34 let counter = document.getElementById("counter") 35 if (counter) { 36 let d = parseduration(counter.dateTime) 37 let total = document.getElementById("total") 38 if (total) 39 updatetime(counter, gettime() - d, total, parseduration(total.dateTime) - d) 40 else 41 updatetime(counter, gettime() - d) 42 }