Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
pCallback |
function | A callback function that is called each time interval and
when the timer changes state.
Function signature: {void|boolean|Promise} callback( {string} reason ); The function receives a single string argument that indicates the reason for the call. It is one of "start", "interval", "pause", "restart", "stop". The return value of the function is ignored except for when the reason is "interval". When the reason is "interval" the return value can be:
When the reason is "interval" and the callback throws an exception the timer will stop as if ForegroundTimer#stop was called. While the timer is running, if the page enters the hidden state (for example when another browser tab is activated) the callback is called with reason = "pause" and the timer is paused until the page is no longer hidden. |
|||||||||||||||||
pOptions |
object |
<optional> |
An options object
Properties
|
Examples
Create a timer that runs every 60 seconds
const t1 = new apex.util.ForegroundTimer( ( reason ) => {
if ( reason === "interval" ) {
console.log( "timer interval", new Date() );
// do some processing
// return false if something goes wrong and the timer needs to stop
} else {
console.log( "timer status change", reason );
}
}, {
interval: 60,
autoStart: true
} );
Create a timer that runs every 30 seconds. In this case the processing is asynchronous and the timer does not auto start.
const t1 = new apex.util.ForegroundTimer( ( reason ) => {
if ( reason === "interval" ) {
return new Promise( resolve => {
// do some processing. This is simulating an async process.
setTimeout( () => {
console.log( "timer interval", new Date() );
resolve( true ); // resolve with false if something goes wrong and the timer must stop
}, 2000 );
} );
} else {
console.log( "timer status change", reason );
}
}, {
interval: 30
} );
// when ready call start
t1.start();
Type:
- boolean
- Default Value:
- {false}
A value of 0 will disable the timer. Other values less than ForegroundTimer#minInterval will be coerced to ForegroundTimer#minInterval.
Type:
- number
- Default Value:
- 5 seconds
Type:
- number
- Default Value:
- 2
Returns the current state of the timer.
Returns:
- Type
- string
Call to pause the running timer.
The difference between pause and stop
is that calling start after pause
will call the callback with reason "interval" sooner because it takes the last time the interval ran
into consideration.
Calling start after stop will wait
the full interval seconds before calling it.
Example
Pause the timer t1.
t1.pause();
// the callback provided when the timer was created is called with the reason = "pause"
// and it is not called again until the timer is started.
Call to start the timer running. While the timer is running the callback is called with reason = "interval" every interval seconds.
Example
Start the timer t1.
t1.start();
// now the callback provided when the timer was created is called first with the reason = "start"
// (or if the timer was previously paused the reason = "restart")
// and then every interval seconds after that with reason = "interval" until the timer is stopped or paused.
Example
Stop the timer t1.
t1.stop();
// the callback provided when the timer was created is called with the reason = "stop"
// and it is not called again until the timer is started.