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
// 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 global override function
setGlobalTimeoutOverrides(customSetTimeout);
// Now all timeout operations will use the custom setTimeout
let timer = scheduleTimeout(() => {
console.log("Timer triggered");
}, 100);
// Reset to native implementations
setGlobalTimeoutOverrides(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 global override functions
setGlobalTimeoutOverrides([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
setGlobalTimeoutOverrides();
Sets the global setTimeout and clearTimeout override functions 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 global overrides to undefined, reverting to native implementations.