Module basic module

module timers

The timers module is a built-in module that provides some time-related functions and objects, including setTimeout(), setInterval(), clearInterval(), clearTimeout(), etc.

The setInterval() function calls the function every specified time and returns the timer object.

1 2 3 4 5 6
var timers = require('timers'); function callback() { console.log('Hello, World!'); } var intervalId = timers.setInterval(callback, 1000);

The setTimeout() function calls the function after the specified time and returns the timer object.

1 2 3 4 5 6
var timers = require('timers'); function callback() { console.log('Hello, World!'); } var timeoutId = timers.setTimeout(callback, 1000);

The clearTimeout() function is used to cancel a timer set by the setTimeout() method. What needs to be provided is the timer identifier value returned in setTimeout().

1 2 3 4 5 6 7
var timers = require('timers'); function callback() { console.log('Hello, World!'); } var timeoutId = timers.setTimeout(callback, 1000); timers.clearTimeout(timeoutId);

The clearInterval() function is used to cancel a timer set by the setInterval() method. What needs to be provided is the timer identifier value returned in setInterval().

1 2 3 4 5 6 7
var timers = require('timers'); function callback() { console.log('Hello, World!'); } var intervalId = timers.setInterval(callback, 1000); timers.clearInterval(intervalId);

The setImmediate() function is used to call the function immediately during the next idle time and return the timer object.

1 2 3 4 5 6
var timers = require('timers'); function callback() { console.log('Hello, World!'); } var immediateId = timers.setImmediate(callback);

The above are examples of common functions and objects provided by the timers module. By using these functions and objects, you can easily implement delayed task execution or scheduled task execution in your application.

static function

setTimeout

Call a function after a specified amount of time

1 2 3
static Timer timers.setTimeout(Function callback, Number timeout = 1, ...args);

Call parameters:

  • callback: Function, specify the callback function
  • timeout: Number, specifies the delay time, in milliseconds. If it exceeds 2^31, it will be executed immediately.
  • args: ..., additional parameters, passed into the specified callback, optional.

Return results:

  • Timer, returns the timer object

clearTimeout

Clear the specified timer

1
static timers.clearTimeout(Value t);

Call parameters:

  • t: Value, specifies the timer to be cleared

setInterval

Call the function after a specified interval

1 2 3
static Timer timers.setInterval(Function callback, Number timeout, ...args);

Call parameters:

  • callback: Function, specify the callback function
  • timeout: Number, specifying the interval time in milliseconds. If it exceeds 2^31, it will be executed immediately.
  • args: ..., additional parameters, passed into the specified callback, optional.

Return results:

  • Timer, returns the timer object

clearInterval

Clear the specified timer

1
static timers.clearInterval(Value t);

Call parameters:

  • t: Value, specifies the timer to be cleared

setHrInterval

The function is called after every specified time interval. This is a high-precision timer that will actively interrupt the execution timer of the running JavaScript script.

1 2 3
static Timer timers.setHrInterval(Function callback, Number timeout, ...args);

Call parameters:

  • callback: Function, specify the callback function
  • timeout: Number, specifying the interval time in milliseconds. If it exceeds 2^31, it will be executed immediately.
  • args: ..., additional parameters, passed into the specified callback, optional.

Return results:

  • Timer, returns the timer object

Since the setHrInterval timer will interrupt the running code execution callback, do not modify data that may affect other modules in the callback function, or call any api function marked async in the callback, otherwise unpredictable results will occur. For example:

1 2 3 4 5 6 7 8 9 10
var timers = require('timers'); var cnt = 0; timers.setHrInterval(() => { cnt++; }, 100); while (cnt < 10); console.error("===============================> done");

In this code, the loop on line 8 will not end because of the change of cnt, because JavaScript will determine that cnt will not be changed during the loop when optimizing the code.


clearHrInterval

Clear the specified timer

1
static timers.clearHrInterval(Value t);

Call parameters:

  • t: Value, specifies the timer to be cleared

setImmediate

Execute the callback function immediately during the next idle time

1 2
static Timer timers.setImmediate(Function callback, ...args);

Call parameters:

  • callback: Function, specify the callback function
  • args: ..., additional parameters, passed into the specified callback, optional.

Return results:

  • Timer, returns the timer object

clearImmediate

Clear the specified timer

1
static timers.clearImmediate(Value t);

Call parameters:

  • t: Value, specifies the timer to be cleared

call

Calls the given function and interrupts the function when the timeout expires

1 2 3
static Value timers.call(Function func, Number timeout, ...args);

Call parameters:

  • func: Function, specifies the function to be run
  • timeout: Number, specify the timeout period
  • args: ..., additional parameters, passed into the specified callback, optional.

Return results:

  • Value, returns the running result of func