New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useThrottle #154
Comments
🎉 This issue has been resolved in version 6.1.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
@streamich unfortunately it's not implemented correctly - instead of discarding the intermediary function calls, it just delays them all. i adapted this code as follows import * as React from 'react';
export const useThrottle = (fn: () => any, ms: number = 0, args?: any) => {
const lastRan = React.useRef(0);
const timeout = React.useRef(0);
const run = React.useCallback(() => {
fn.apply(null, args);
lastRan.current = Date.now();
}, [lastRan, fn, args]);
React.useEffect(() => {
clearTimeout(timeout.current);
const diff = Date.now() - lastRan.current;
if (diff >= ms) {
run();
} else {
timeout.current = setTimeout(run, ms - diff);
}
return () => { clearTimeout(timeout.current) }
}, args);
}; |
@gausie thanks, I will add this fix |
🎉 This issue has been resolved in version 6.2.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
It would be nice to have useThrottle, implemented similarly to useDebounce
The text was updated successfully, but these errors were encountered: