Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Н Ё Х
- const EventEmiter = require('events');
- class Timer extends EventEmiter {
- constructor(total) {
- super();
- this.total = total;
- this.ticks = 0;
- }
- start() {
- this.interval = setInterval(() => this.tick(), 1000);
- this.emit('start');
- }
- tick() {
- this.ticks += 1;
- if (this.ticks <= this.total) {
- this.emit('tick', this.ticks);
- } else {
- this.end();
- }
- }
- end() {
- clearInterval(this.interval);
- this.emit('end');
- }
- }
- const timer = new Timer(10);
- timer.once('start', function () {
- console.log('Start');
- });
- timer.on('tick', function (tick) {
- console.log(tick);
- });
- timer.once('end', function () {
- console.log('End');
- });
- timer.start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement