JSFiddle - React, Tailwind, and code Playground
HTML
<button id="button">Click me!</button>
<div id="results"></div>
JavaScript
var Timer = function (interval) {
var me = this;
var timeout = null;
me.interval = interval;
me.tick = function () {};
me.reset = function () {
if (timeout != null) {
clearTimeout(timeout);
}
timeout = setTimeout(function () {
me.tick();
timeout = null;
me.reset();
}, me.interval);
}
me.start = function () {
me.reset();
}
me.stop = function () {
clearTimeout(timeout);
}
}
function addResult() {
$('#results').append("<div>Tick!</div>");
}
var myTimer = new Timer(5000);
myTimer.tick = addResult;
$('#button').on('click', function() {
addResult();
myTimer.reset();
});
myTimer.start();