hd-utils
Version:
A handy utils for modern JS developers
15 lines (14 loc) • 401 B
JavaScript
/**
* @description a cleaner way to use setTimeout with ability to clear timeout wihtout saving the timeoutId;
* @example `const cancelTimeout = onTimeout(100, ()=>{
* // Do something here
* });
*
* // on destory.
* cancelTimeout();
* `
*/
export default function onTimeout(timeout, callback) {
const timeoutId = setTimeout(callback, timeout);
return () => clearTimeout(timeoutId);
}