@anonymous/

DependentFreeChapters

Nodejs

No description

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const throttle = require('lodash.throttle')

const events = [];
let count = 0;
const processEventsNow = () => {
  // do something with the events ... 
  count += events.length;
  events.length = 0;
} 
const processEvents = throttle(processEventsNow, 10);

const gotEvent = (e) => {
  events.push(e);
  processEvents();
} 

let intervalCalls = 10;
let interval = setInterval(() => {
  for(let i=0; i<10; i++) {
    gotEvent(i)
  }
  
  processEventsNow();
  console.log(`count=${count}`);
  if(intervalCalls-- < 1) {
    clearInterval(interval);
  }
}, 9);