Spaces:
Sleeping
Sleeping
File size: 573 Bytes
4a288a7 | 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 | // @flow strict
/**
* Throttle the given function to run at most every `period` milliseconds.
* @private
*/
export default function throttle(fn: () => void, time: number): () => ?TimeoutID {
let pending = false;
let timerId: ?TimeoutID = null;
const later = () => {
timerId = null;
if (pending) {
fn();
timerId = setTimeout(later, time);
pending = false;
}
};
return () => {
pending = true;
if (!timerId) {
later();
}
return timerId;
};
}
|