JSFiddle - React, Tailwind, and code Playground

by brigand

JavaScript

function Timer(interval, callback, run){
    this.interval = interval;
    this.callback = callback;
    
    if (run) {
        this.resume();
    }
}

Timer.prototype.pause = function(){
    if (this._timer) {
        clearInterval(this._timer);    
    }
};

Timer.prototype.resume = function(){
    if (!this._timer) {
        this._timer = setInterval(this.callback, this.interval);
    }
}