Skip to content

Instantly share code, notes, and snippets.

@gengkev
Created January 7, 2012 03:37
Show Gist options
  • Save gengkev/1573701 to your computer and use it in GitHub Desktop.
Save gengkev/1573701 to your computer and use it in GitHub Desktop.
A very oddly worded setTimeout replacement that uses a convoluted algorithm to try to run as close as possible to the specified interval.
function BirsdayTimer (interval,checkUpdateTime,nTimeWeight,func) {
this.func = func;
this.nTimeWeight = nTimeWeight;
this.interval = interval;
this.checkUpdateTime = checkUpdateTime;
}
BirsdayTimer.prototype.haisur = function(){
this.execTime = this.execTime || +new Date(),
this.updateTime = this.updateTime || this.execTime,
curTime = +new Date(),
this.averageDelay = this.averageDelay || 50;
this.averageDelay *= 1 - this.nTimeWeight;
this.averageDelay += (curTime - this.updateTime) * this.nTimeWeight;
var timeUntilTick = (this.execTime + this.interval) - curTime;
if (timeUntilTick <= this.averageDelay) {
this.func.call(window);
this.execTime += this.interval;
this.updateTime = this.execTime;
timeUntilTick = this.interval;
}
var newUpdateTime = timeUntilTick * this.checkUpdateTime - this.averageDelay;
(function(_this){
_this._s = setTimeout(function(){_this.haisur()},newUpdateTime);
})(this);
this.updateTime += newUpdateTime;
}
BirsdayTimer.prototype.stawp = function(tm) {
if (tm != "tm") {
this.stawp("tm");
return;
}
clearTimeout(this._s);
this.execTime = this.updateTime = this.averageDelay = undefined;
}
@gengkev
Copy link
Author

gengkev commented Jan 7, 2012

Unlike the other gist that I have and is forked of manast's, this BirsdayTimer tries to instead mantain running as close as possible to the interval, rather than keeping everything in line overall. For that reason, there is little reason to use this API, combined with its esoteric naming scheme, its weird algorithms, and its greater CPU use. Usage:

// initialize the timer
var timer = new BirsdayTimer(interval, checkUpdateTime, nTimeWeight, func);

// run the timer
timer.haisur();

// stop the timer
timer.stawp("tm"); // "tm" is optional

interval: How often should the timer execute?
checkUpdateTime: a value between 0 and 1 that determines how long (in percent of the interval) before the next interval the BirsdayTimer should check and update itself.
nTimeWeight: The BirsdayTimer calculates an average of how much the setTimeout delay is. It doesn't calculate the true average, it simply calculates the average of the old average and the new delay measure (initial value: 50ms). Set this attribute to specify what weight should be given to the new delay when averaging.
func: what function would you like to call? The function will have its this object set to the window object.

Suggested values (function will break if not inputted): checkUpdateTime = 0.4; nTimeWeight = 0.1 (Feel free to experiment.)

Example usage:

var time = +new Date(),
  interval = new BirsdayTimer(1000, 0.4, 0.1, function() {
    var n = +new Date();
    console.log(n - time);
    time = n;
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment