UNPKG

pgh-common-utils

Version:

A collection of utility functions for TypeScript.

29 lines (28 loc) 711 B
// 防抖 export function debounce(func, delay = 300) { let timer; return (...args) => { clearTimeout(timer); timer = setTimeout(() => func(...args), delay); }; } // 节流 export function throttle(func, limit = 300) { let lastFunc; let lastRan; return (...args) => { if (!lastRan) { func(...args); lastRan = Date.now(); } else { clearTimeout(lastFunc); lastFunc = setTimeout(() => { if (Date.now() - lastRan >= limit) { func(...args); lastRan = Date.now(); } }, limit - (Date.now() - lastRan)); } }; }