Optional
overrideFn: TimeoutOverrideFn | TimeoutOverrideFuncssetTimeout override function this will be called instead of the setTimeout
, if the value
of overrideFn
is null or undefined it will reset both setTimeout and clearTimeout to their native implementations.
May also be an array with both the setTimeout and clearTimeout override functions, if either is not provided the
default native functions will be used
These override functions apply only to this package instance and do not affect the global setTimeout/clearTimeout functions used elsewhere in your application.
// Override with a single function for setTimeout
// The native clearTimeout will still be used
function customSetTimeout<TArgs extends any[]>(callback: (...args: TArgs) => void, ms?: number, ...args: TArgs) {
console.log(`Setting timeout for ${ms}ms`);
return setTimeout(callback, ms);
}
// Set the timeout override function
setTimeoutOverrides(customSetTimeout);
// Now all timeout operations will use the custom setTimeout
let timer = scheduleTimeout(() => {
console.log("Timer triggered");
}, 100);
// Reset to native implementations
setTimeoutOverrides(undefined);
// Override both setTimeout and clearTimeout with custom implementations
function customSetTimeout<TArgs extends any[]>(callback: (...args: TArgs) => void, ms?: number, ...args: TArgs) {
console.log(`Setting timeout for ${ms}ms`);
return setTimeout(callback, ms);
}
function customClearTimeout(timeoutId: number) {
console.log(`Clearing timeout ${timeoutId}`);
return clearTimeout(timeoutId);
}
// Set both override functions
setTimeoutOverrides([customSetTimeout, customClearTimeout]);
// Now all timeout operations will use the custom implementations
let timer = scheduleTimeout(() => {
console.log("Timer triggered");
}, 100);
// This will use the custom clearTimeout
timer.cancel();
// Reset to native implementations
setTimeoutOverrides();
Sets the setTimeout and clearTimeout override functions for this package/closure instance to be used by all timeout operations when no specific override functions are provided. If called with no parameters or undefined, it will reset both overrides to undefined, reverting to native implementations.