03. Timers
The timers object is a built-in object that provides timer functionality.
Functional overview:
- Provides a one-shot timer and a periodic timer.
Limitations:
- The number of timer resources that can be used simultaneously is 5.
- Resource errors cause exceptions.
timers Global Object
When using timer functions, the instance name 'timers'
can be omitted.
Methods()/Properties | Summary | Version | Note |
---|---|---|---|
setTimeout() | Starts the one-shot timer. | 01.00.00+ | A {Timeout} is generated. |
clearTimeout() | Stops the one-shot timer. | 01.00.00+ | |
setInterval() | Starts the periodic timer. | 01.00.00+ | A {Timeout} is generated. |
clearInterval() | Stops the periodic timer. | 01.00.00+ | |
insertDelay() | Inserts a delay of the specified time. | 01.01.01+ |
Details
setTimeout([callback,]delay)
Starts the one-shot timer.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
callback() | function | optional | Executes callback processing when a timeout occurs. | |
delay | number | mandatory | Specify the time [ms] until a timeout occurs. Range: 1 - 4,294,967,295 | Values above the maximum range are treated as maximum. The timer can be set from 1ms, but there is a delay due to internal processing. Please use in consideration of time lag. |
return | {Timeout} | - | {Timeout} : Generated {Timeout} | When an error occurs, an exception is raised. |
clearTimeout(timeout)
Stops the one-shot timer.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
timeout | {Timeout} | mandatory | Specify the {Timeout} object generated by setTimeout(). | If null is specified, it is ignored. |
return | undefined | - | - |
setInterval(callback,delay)
Starts the periodic timer.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
callback() | function | mandatory | Executes callback processing when a timeout occurs. | |
delay | number | mandatory | Specify the time [ms] until a timeout occurs. Range: 1 - 4,294,967,295 | Values above the maximum range are treated as maximum. The timer can be set from 1ms, but there is a delay due to internal processing. Please use in consideration of time lag. |
return | {Timeout} | - | {Timeout} : Generated {Timeout} | When an error occurs, an exception is raised. |
clearInterval(timeout)
Stops the periodic timer.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
timeout | {Timeout} | mandatory | Specify the {Timeout} object generated by setInterval(). | If null is specified, it is ignored. |
return | undefined | - | - |
insertDelay(delay)
Inserts a delay of the specified time.
At that time, all processing is temporarily completely blocked.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
delay | number | mandatory | Specify the delay time [ms]. Range: 1 - 100 | The timer can be set from 1ms, but there is a delay due to internal processing. Please use in consideration of time lag. |
return | undefined | - | - | When an error occurs, an exception is raised. |
Usage example:
insertDelay(10); //10msec wait
{Timeout}
This object is internally generated by setTimeout()/setInterval() and returned.
Methods()/Properties | Summary | Version | Note |
---|---|---|---|
.wait() | Waits (blocks) until the timer resource is released. | 01.00.00+ |
Details
.wait()
Waits (blocks) until the timer is deactivated and resources are released.
Do not use this method in the callback process of the {Timeout} object generated by specifying the callback function. Also, do not use this method on {Timeout} objects generated by setInterval().
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
return | undefined | - |
Usage example:
setTimeout(1000).wait(); //1sec wait
This method blocks until the timer resource is released.
If the timer is repeatedly turned on and off continuously, the number of timers will exceed the allowable amount and an exception may occur.
The following is an extreme example but can be used to avoid the problem of exceeding the allowable number of timers when continuously turning on and off timers.
while(1) {
tmo = setTimeout(function() { print('timeout!'); }, 30000);
clearTimeout(tmo);
tmo.wait();
}
Object Usage Examples
Sample 1
This is a sample of one-shot timer and periodic timer.
Starts a 1-second periodic timer and outputs 'Hello JS World' every time the timer expires.
Finally, wait for 15 seconds and stop the periodic timer.
var tmo = setTimeout(function() {
print('timeout!');
}, 5000);
var tocnt = 0;
var tmobj = setInterval(function() {
print('Hello JS World:' + tocnt++);
}, 1000);
setTimeout(15000).wait();
clearInterval(tmobj);
print('Good see you later!');
Sample 2
This is a sample of a periodic timer.
Start a 1-second period timer and restart the one-shot timer each time it expires.
var cbFlag = 0;
var tmOne = null;
var tmInterval = setInterval(function() {
if(tmOne && cbFlag == 0) {
print('clear one-shot timer');
cbFlag = 1;
}
}, 1000);
while(1) {
if(cbFlag) {
tmOne.wait();
tmOne = null;
cbFlag = 0;
}
if(!tmOne) {
print('start one-shot timer');
tmOne = setTimeout(function() {
print('timeout!');
}, 1000);
}
}