Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. // Н Ё Х
  2. const EventEmiter = require('events');
  3.  
  4. class Timer extends EventEmiter {
  5. constructor(total) {
  6. super();
  7. this.total = total;
  8. this.ticks = 0;
  9. }
  10.  
  11. start() {
  12. this.interval = setInterval(() => this.tick(), 1000);
  13. this.emit('start');
  14. }
  15.  
  16. tick() {
  17. this.ticks += 1;
  18. if (this.ticks <= this.total) {
  19. this.emit('tick', this.ticks);
  20. } else {
  21. this.end();
  22. }
  23. }
  24.  
  25. end() {
  26. clearInterval(this.interval);
  27. this.emit('end');
  28. }
  29.  
  30. }
  31.  
  32.  
  33. const timer = new Timer(10);
  34.  
  35. timer.once('start', function () {
  36. console.log('Start');
  37. });
  38.  
  39. timer.on('tick', function (tick) {
  40. console.log(tick);
  41. });
  42.  
  43. timer.once('end', function () {
  44. console.log('End');
  45. });
  46.  
  47. timer.start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement