Skip to content

Instantly share code, notes, and snippets.

@syul
Created January 30, 2017 11:10
Show Gist options
  • Save syul/91f0a3583eb7a8719834137c691841b1 to your computer and use it in GitHub Desktop.
Save syul/91f0a3583eb7a8719834137c691841b1 to your computer and use it in GitHub Desktop.
Clock implementation
var Test = function () {
this.eventsStorage = [];
this.loopHendler = undefined;
};
Test.prototype.on = function (name, cb) {
this.eventsStorage.push({
name: name,
fn: cb
})
};
Test.prototype.invoke = function (eventName, args) {
this.eventsStorage
.filter(function (el) {
return el.name == eventName;
})
.forEach(function (el) {
el.fn.call(null, args);
});
}
Test.prototype.startLoop = function (interval) {
var eventName = 'tick';
this.loopHendler = setInterval(function () {
this.invoke(eventName, 'it is working');
}.bind(this), interval);
};
Test.prototype.stopLoop = function () {
clearInterval(this.loopHendler);
};
var t = new Test();
t.on('sec', function (arg) {
console.log(arg);
});
t.on('min', function (arg) {
console.log(arg);
});
t.on('hour', function (arg) {
console.log(arg);
});
console.log('1111');
t.startLoop(1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment