Skip to content

Instantly share code, notes, and snippets.

@romasan
Last active November 15, 2019 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romasan/d0ff4f32a5246743f0a9478f433c1d88 to your computer and use it in GitHub Desktop.
Save romasan/d0ff4f32a5246743f0a9478f433c1d88 to your computer and use it in GitHub Desktop.
const peakBounce = (interval, count) => {
let iCount = 0;
let stack = [];
setInterval(() => {
const part = stack.splice(0, count);
part.forEach(fn => fn())
iCount = part.length;
}, interval);
return (fn, ...a) => {
if (typeof fn !== 'function') {return}
if (iCount < count) {
iCount++;
fn(...a);
} else {
stack.push(() => {
fn(...a);
})
}
}
}
const pb = peakBounce(1000, 2);
const f = i => {console.log(i)}
pb(f, 1);
pb(f, 2);
pb(f, 3);
pb(f, 4);
pb(f, 5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment