Skip to content

Commit

Permalink
3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
odoldotol committed Jul 5, 2023
1 parent f71fe5f commit 925df59
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions 03-callbacks-and-events/ticker.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { EventEmitter } from 'events';

/**
*
* @param {number} number
* @param {(n:number)=>any} callback
*/
function ticker(number, callback) {
const emitter = new EventEmitter();
let tick_count = 0;

setTimeout(() => callback(tick_count), number);

const ticker50 = () => {
setTimeout(() => {
emitter.emit('tick');
tick_count++;
ticker50();
}, 50);
};

ticker50();

return emitter;
}

const runningTicker = ticker(1000, (tick_count) => {
console.log(tick_count);
});

runningTicker.on('tick', () => console.log('tick'));

0 comments on commit 925df59

Please sign in to comment.