JSFiddle - React, Tailwind, and code Playground
by Roman Zhak
JavaScript
+function(){
var self = this;
var TimeFx = function() {
this.__timersList = {};
this.__index = 0;
}
TimeFx.prototype.set = function(over, callback, type, __re) {
var intervalID
, saveIndex = (__re || __re === 0 ) || this.__index++;
intervalID = self['set' + (type ?
'Timeout' : 'Interval')](callback, over);
this.__timersList[saveIndex] = {
id : intervalID,
target : !!type,
isCalling: callback,
delay : over,
type : typeof callback === 'string'
? 'code' : 'function'
}
return saveIndex;
}
TimeFx.prototype.stop = function(id, drop) {
var __this = this.__timersList[id];
self['clear' + (__this.target ?
'Timeout' : 'Interval')](__this.id);
return this;
}
TimeFx.prototype.run = function(id) {
if( id in this.__timersList ) {
var __this = this.__timersList[id];
console.log(id)
this.set(
__this.delay + 1000 ,
__this.isCalling ,
__this.target , id
)
}
return this;
}
this.TimeFx = TimeFx;
}.call(this);
var timer = new TimeFx();
var id = timer.set(1000, main);
function main() {
console.log(new Date)
}
console.log(timer)
timer.set(3000, function(){
timer.stop(id);
timer.set(3000, function(){
// console.log(timer.run(id))
}, true);
}, true)