@rbxts/jsnatives
clearTimeout
Cancels a timeout that was set with setTimeout
.
Signature
function clearTimeout(sym: symbol | undefined): void
Description
The clearTimeout
function cancels a previously scheduled timeout that was created using setTimeout
. Once a timeout is cleared, its callback function will not be executed.
If the provided symbol is undefined or the timeout has already executed, the function has no effect.
Parameters
sym
: The symbol that identifies the timeout to cancel, returned bysetTimeout
. Can be undefined.
Return value
- None
Examples
Basic usage
// Create a timeoutconst timeoutSymbol = setTimeout((sym) => { print("This will not be printed");}, 5000);
// Cancel the timeout before it executesclearTimeout(timeoutSymbol);
Handling undefined symbols
// clearTimeout safely handles undefined symbolslet maybeTimeout: symbol | undefined;
if (Math.random() > 0.5) { maybeTimeout = setTimeout((sym) => { print("Random timeout executed"); }, 1000);}
// This is safe even if maybeTimeout is undefinedclearTimeout(maybeTimeout);
Creating a cancellable operation
// Create a function that starts an operation that can be cancelledfunction startCancellableOperation() { let timeoutSymbol: symbol | undefined;
const start = () => { print("Operation started"); timeoutSymbol = setTimeout((sym) => { print("Operation completed"); timeoutSymbol = undefined; }, 5000); };
const cancel = () => { if (timeoutSymbol) { print("Operation cancelled"); clearTimeout(timeoutSymbol); timeoutSymbol = undefined; } };
return { start, cancel };}
const operation = startCancellableOperation();operation.start();// Later:operation.cancel();