maplibre-gl
Version:
BSD licensed community fork of mapbox-gl, a WebGL interactive maps library
25 lines (22 loc) • 574 B
text/typescript
/**
* Throttle the given function to run at most every `period` milliseconds.
*/
export function throttle(fn: () => void, time: number): () => ReturnType<typeof setTimeout> {
let pending = false;
let timerId: ReturnType<typeof setTimeout> = null;
const later = () => {
timerId = null;
if (pending) {
fn();
timerId = setTimeout(later, time);
pending = false;
}
};
return () => {
pending = true;
if (!timerId) {
later();
}
return timerId;
};
}