I've recently came across many inconsistencies in a code which I had to maintain and develop. Mainly in ajax and timing/interval functions. I'm quite a novice in the ways of JS but tried my best to standarize those into one timer class with passable functions for specific inpage changes since It was supposed to be universal app-wide. The class is working nicely and I'm quite happy with it but would love some tips on how to improve it further.
/**
* Timer class
* Ajax functions have to set self.stillPending before start and zero it after the responce has been received for the class to work properly.
* @param function startFunc Function used when starting the timer
* @param function updateFunc Function used every cycle
* @param function endFunc Function used when stopping the timer
* @param integer refreshDelay Delay between update cycles (in seconds) (default 5)
* @param function timerFunc Function used every second (to update GUI timers etc. SHOULDNT EXECUTE ANYTHING PROCESSING HEAVY I.E AJAX)
* @param boolean pregeneration Execute one update before the timer start to predownload data
* @returns Timer Class Object
*/
class Timer {
constructor(startFunc, updateFunc, endFunc, refreshDelay = 5, timerFunc, pregeneration = false) {
this.startFunc = startFunc;
this.endFunc = endFunc;
this.updateFunc = updateFunc;
this.timerFunc = timerFunc;
this.refreshDelay = refreshDelay;
this.time = 0;
this.timeCycle;
this.cycle;
this.tempData;
this.stillPending = 0;
this.isDisabled = 0;
this.inversion = 0;
this.pregeneration = pregeneration;
}
updateData() {
var self = this;
if (self.updateFunc !== null) {
if (self.stillPending === 0) {
self.updateFunc();
}
}
}
restartCycle() {
var self = this;
if (self.cycle > 0) {
self.stopCycle();
}
self.startCycle();
}
startCycle() {
var self = this;
self.isDisabled = 0;
if (!self.cycle) {
if (self.startFunc !== null) {
self.startFunc();
}
if (self.refreshDelay !== 1) {
self.timerCycle = setInterval(function () {
self.time = (self.time + 1);
if (self.timerFunc !== null) {
self.timerFunc();
}
}, 1000);
}
if (self.pregeneration === true) {
self.updateData();
}
self.cycle = setInterval(function () {
if (self.updateFunc !== null) {
self.updateData();
}
if (self.refreshDelay === 1) {
self.time = (self.time + 1);
if (self.timerFunc !== null) {
self.timerFunc();
}
}
}, (self.refreshDelay * 1000));
}
}
stopCycle() {
var self = this;
if (self.cycle) {
if (self.endFunc !== null) {
self.endFunc();
}
self.isDisabled = 1;
clearInterval(self.cycle);
clearInterval(self.timeCycle);
self.cycle = null;
self.timeCycle = null;
}
}
}
Its intended example usage would be
var statisticsTimer = new Timer(null, function(){ //updatefunc var self = this; self.stillPending = 1; [..]ajax[..].then(function(data){ [..]stuff to do[..] self.stillPending = 0; }); }, null, 30, null, true); $(document).ready(function(){ statisticsTimer.startCycle(); });
Which would pregenerate data before interval with set update function and execute it every cycle ie every 30sec.
Other possible unpassed functions would mainly do other magic if needed on page such as predefine containers so the update function actually just updates data without any heavy html editing.
tempData
is for some dynamic data holding within class between cycles or so.