// sets loop to zero
var totalTenths = 0;
var interval;
var startButton = document.querySelector('#start');
// start and pause button
document.querySelector('#start').addEventListener('click', function (e) {
var startButton = e.target;
if (startButton.innerHTML === 'Start') {
startButton.innerHTML = 'Pause';
interval = setInterval(countTimer, 10)
colorInterval = setInterval(colorTimer, 1000)
}
else if (e.target.innerHTML === 'Pause') {
startButton.innerHTML = 'Resume';
clearInterval(interval);
clearInterval(colorInterval);
// here I'm setting the 15 second restart interval
waitedTooLong = setInterval(timeout, 15000)
}
else if (startButton.innerHTML === 'Resume') {
startButton.innerHTML = 'Pause';
interval = setInterval(countTimer, 10)
colorInterval = setInterval(colorTimer, 1000)
}
});
// double click to clear function
document.querySelector('#start').addEventListener('dblclick', function(e) {
var startButton = e.target;
if (startButton.innerHTML === 'Resume') {
clearInterval(function() {
setInterval(countTimer, 10)
});
document.getElementById('tenths').innerHTML = '00';
document.getElementById('seconds').innerHTML = '00';
document.getElementById('minutes').innerHTML = '00';
document.getElementById('start').innerHTML = 'Start'
}
});
// loop that converts 10th of millisec to minute and seconds
function countTimer() {
totalTenths++;
var minutes = Math.floor(totalTenths / 6000);
var seconds = Math.floor((totalTenths - minutes * 6000) / 100);
var tenths = totalTenths - (minutes * 6000 + seconds * 100);
// replaces inner html with loop with added zero until double digits accure
if (tenths > 0) {
document.getElementById('tenths').innerHTML = '0' + tenths;
}
if (tenths > 9) {
document.getElementById('tenths').innerHTML = tenths;
}
if (tenths > 9) {
document.getElementById('seconds').innerHTML = '0' + seconds;
}
if (seconds > 9) {
document.getElementById('seconds').innerHTML = seconds;
}
if (tenths > 0) {
document.getElementById('minutes').innerHTML = '0' + minutes;
}
if (minutes > 9) {
document.getElementById('minutes').innerHTML = minutes;
}
}
<div class="text-center container">
<button id="start" class="btn btn-large btn-success">Start</button>
<p class="clear-msg">double click to clear!</p>
<div id="timer" class="well">
<span id="minutes">00</span>:<span id="seconds">00</span>:<span id="tenths">00</span>
</div>
</div>
I have a setInterval called Interval which runs a countup timer. I have a start button that plays on first initial click and pauses on second just fine. When I double click, it displays the timer back to zero, but doesn't seem to be clearing the actual timer. Will just play where it was left off before the display was replaced with zeros.