vuecssuiant-designantdreactantantd-vueenterprisefrontendui-designvue-antdvue-antd-uivue3vuecomponent
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
613 B
29 lines
613 B
import raf from './raf'; |
|
|
|
type throttledFn = (...args: any[]) => void; |
|
|
|
type throttledCancelFn = { cancel: () => void }; |
|
|
|
function throttleByAnimationFrame<T extends any[]>(fn: (...args: T) => void) { |
|
let requestId: number | null; |
|
|
|
const later = (args: T) => () => { |
|
requestId = null; |
|
fn(...args); |
|
}; |
|
|
|
const throttled: throttledFn & throttledCancelFn = (...args: T) => { |
|
if (requestId == null) { |
|
requestId = raf(later(args)); |
|
} |
|
}; |
|
|
|
throttled.cancel = () => { |
|
raf.cancel(requestId!); |
|
requestId = null; |
|
}; |
|
|
|
return throttled; |
|
} |
|
|
|
export default throttleByAnimationFrame;
|
|
|