mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-29 23:54:24 +00:00
24 lines
676 B
TypeScript
24 lines
676 B
TypeScript
/**
|
|
* Copyright (c) 2021 GraphQL Contributors.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
/**
|
|
* Provided a duration and a function, returns a new function which is called
|
|
* `duration` milliseconds after the last call.
|
|
*/
|
|
export default function debounce<F extends (...args: any[]) => any>(duration: number, fn: F) {
|
|
let timeout: number | null;
|
|
return function (this: any, ...args: Parameters<F>) {
|
|
if (timeout) {
|
|
window.clearTimeout(timeout);
|
|
}
|
|
timeout = window.setTimeout(() => {
|
|
timeout = null;
|
|
fn.apply(this, args);
|
|
}, duration);
|
|
};
|
|
}
|