JSFiddle - React, Tailwind, and code Playground
HTML
<input type="submit" name="clear" value="Stop Timer" />
<input type="submit" name="reset" value="Restart Timer" />
<span id="count">0</span>
<input type="submit" name="increment" value="Increment" />
JavaScript
// Vars
var timerSpeed = 3000 // 3 seconds, for debugging purposes
// Set timer on window load
window.t = setInterval(function() {
$('#count').text(parseInt($('#count').text()) + 1);
},timerSpeed);
// Manual increment
$('input[name=increment]').click(function() {
// Reset timer
window.clearInterval(t);
window.t = setInterval(function() {
$('#count').text(parseInt($('#count').text()) + 1);
},timerSpeed);
// Update count
$('#count').text(parseInt($('#count').text()) + 1);
});
// Clear timer on "Clear"
$('input[name=clear]').click(function() {
window.clearInterval(t);
});
// Restart timer on "Restart"
$('input[name=reset]').click(function() {
window.t = setInterval(function() {
$('#count').text(parseInt($('#count').text()) + 1);
},timerSpeed);
});