Skip to content

Instantly share code, notes, and snippets.

@vldvel
Created February 28, 2018 08:50
Show Gist options
  • Save vldvel/1f15993b8fd0c0a1c59cea719a051d9a to your computer and use it in GitHub Desktop.
Save vldvel/1f15993b8fd0c0a1c59cea719a051d9a to your computer and use it in GitHub Desktop.
function * throttle(func, time) {
let timerID = null;
function throttled(arg) {
clearTimeout(timerID);
timerID = setTimeout(func.bind(window, arg), time);
}
while (true)
throttled(yield);
}
const thr = throttle(console.log, 1000);
thr.next(); // {value: undefined, done: false}
thr.next('hello'); // {value: undefined, done: false} + 1s after -> 'hello'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment