JSFiddle - React, Tailwind, and code Playground
by luwes
HTML
<button type="button" id="toggle">Play/Pause</button>
JavaScript
let lastIntervalTime = Date.now();
let sessionPlaybackDuration = 0;
let currentlyPlaying = false;
document.querySelector('#toggle').onclick = () => {
return currentlyPlaying ? pause() : play();
};
function play() {
currentlyPlaying = true;
}
function pause() {
currentlyPlaying = false;
}
function timeSince(time) {
return performance.now() - time;
}
setInterval(() => {
if (currentlyPlaying) {
sessionPlaybackDuration += timeSince(lastIntervalTime) / 1000;
}
lastIntervalTime = performance.now();
console.log(sessionPlaybackDuration)
}, 500);