Created
January 30, 2017 11:10
-
-
Save syul/91f0a3583eb7a8719834137c691841b1 to your computer and use it in GitHub Desktop.
Clock implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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