Skip to content

Instantly share code, notes, and snippets.

@raldred
Created November 4, 2013 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raldred/7301769 to your computer and use it in GitHub Desktop.
Save raldred/7301769 to your computer and use it in GitHub Desktop.
Simple interval timer for IGE based on the engine's tickDelta
var IntervalTimer;
IntervalTimer = IgeEntity.extend({
classId: 'IntervalTimer',
init: function() {
IgeEntity.prototype.init.call(this);
this._acc = 0;
this._duration = 0;
this._callback = function() {
return this.log('no callback provided');
};
this._paused = true;
return this.mount(ige._currentViewport);
},
duration: function(val) {
if (typeof val !== 'undefined') {
this._duration = val;
return this;
} else {
return this._duration;
}
},
"do": function(val) {
if (typeof val !== 'undefined') {
this._callback = val;
return this;
} else {
return this._callback;
}
},
pause: function(val) {
return this._paused = !!val;
},
stop: function() {
this._paused = true;
return this.reset();
},
start: function() {
this._paused = false;
return this._callback.call(this);
},
reset: function() {
this._acc = 0;
return this._paused = true;
},
tick: function(ctx) {
if (this._paused) {
return;
}
this._acc += ige._tickDelta;
if (this._acc > this._duration) {
this._acc -= this._duration;
this._callback.call(this);
}
return IgeEntity.prototype.tick.call(this, ctx);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment