Creates and starts a timer which executes a function or specified piece of code once the timer expires. The overrideFn will be
used to create the timer, this is simular to using setTimeout
but provides a return object for cancelling and restarting
(refresh) the timer.
The timer may be cancelled (cleared) by calling the cancel()
function on the returned ITimerHandler, or
you can "reschedule" and/or "restart" the timer by calling the refresh()
function on the returned ITimerHandler
instance
setTimeout override function this will be called instead of the setTimeout
, if the value
of overrideFn
is null or undefined it will revert back to the native setTimeout
. May also be an array with contains
both the setTimeout and clearTimeout override functions, if either is not provided the default native functions will be used
The function to be executed after the timer expires.
Rest
...args: AThe time, in milliseconds that the timer should wait before the specified function or code is executed. If this parameter is omitted, a value of 0 is used, meaning execute "immediately", or more accurately, the next event cycle.
Rest
...args: AAdditional arguments which are passed through to the function specified by callback
.
A ITimerHandler instance which can be used to cancel the timeout.
let timeoutCalled = false;
// Your own "setTimeout" implementation to allow you to perform additional operations or possible wrap
// the callback to add timings.
function newSetTimeoutFn(callback: TimeoutOverrideFn) {
overrideCalled ++;
return setTimeout(callback, timeout);
}
let theTimeout = scheduleTimeoutWith(newSetTimeoutFn, () => {
// This callback will be called after 100ms as this uses setTimeout()
timeoutCalled = true;
}, 100);
// Instead of calling clearTimeout() with the returned value from setTimeout() the returned
// handler instance can be used instead to cancel the timer
theTimeout.cancel();
theTimeout.enabled; // false
// You can start the timer via enabled
theTimeout.enabled = true;
// You can also "restart" the timer, whether it has previously triggered not not via the `refresh()`
theTimeout.refresh();
let timeoutCalled = false;
// Your own "setTimeout" implementation to allow you to perform additional operations or possible wrap
// the callback to add timings.
function newSetTimeoutFn(callback: TimeoutOverrideFn) {
overrideCalled ++;
return setTimeout(callback, timeout);
}
// Your own "clearTimeout" implementation to allow you to perform additional operations or possible wrap
// the callback to add timings.
function newClearTimeoutFn(timeoutId: number) {
overrideCalled ++;
return clearTimeout( timeout);
}
let theTimeout = scheduleTimeoutWith([newSetTimeoutFn, newClearTimeoutFn], () => {
// This callback will be called after 100ms as this uses setTimeout()
timeoutCalled = true;
}, 100);
// Instead of calling clearTimeout() with the returned value from setTimeout() the returned
// handler instance can be used instead to cancel the timer, internally this will call the newClearTimeoutFn
theTimeout.cancel();
theTimeout.enabled; // false
// You can start the timer via enabled
theTimeout.enabled = true;
// You can also "restart" the timer, whether it has previously triggered not not via the `refresh()`
theTimeout.refresh();
Creates and starts a timer which executes a function or specified piece of code once the timer expires. The overrideFn will be used to create the timer, this is simular to using
setTimeout
but provides a return object for cancelling and restarting (refresh) the timer.The timer may be cancelled (cleared) by calling the
cancel()
function on the returned ITimerHandler, or you can "reschedule" and/or "restart" the timer by calling therefresh()
function on the returned ITimerHandler instanceSince
0.4.4
Param: overrideFn
setTimeout override function this will be called instead of the
setTimeout
, if the value ofoverrideFn
is null or undefined it will revert back to the nativesetTimeout
. May also be an array with contains both the setTimeout and clearTimeout override functions, if either is not provided the default native functions will be usedParam: callback
The function to be executed after the timer expires.
Param: timeout
The time, in milliseconds that the timer should wait before the specified function or code is executed. If this parameter is omitted, a value of 0 is used, meaning execute "immediately", or more accurately, the next event cycle.
Param: args
Additional arguments which are passed through to the function specified by
callback
.Returns
A ITimerHandler instance which can be used to cancel the timeout.
Example
Example