Module basic module

Module timers

timers module

Static function

setTimeout

Call the function after the specified 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, execute it immediately.
  • args : ..., additional parameters, passed into the specified callback, optional.

Return result:

  • Timer , returns the timer object

clearTimeout

Clear the specified timer

1
static timers.clearTimeout(Value t);

Call parameters:

  • t : Value, specify the timer to be cleared

setInterval

Call the function after every specified time interval

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

Call parameters:

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

Return result:

  • Timer , returns the timer object

clearInterval

Clear the specified timer

1
static timers.clearInterval(Value t);

Call parameters:

  • t : Value, specify 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 running JavaScript script execution timer

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

Call parameters:

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

Return result:

  • Timer , returns the timer object

Since the timer of setHrInterval will interrupt the running code to execute the callback, do not modify the data that may affect other modules in the callback function, or call any api function marked as async in the callback, otherwise it will produce unpredictable results. E.g:

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 does not end because of the change of cnt, because JavaScript will determine that cnt will not be changed during this cycle when optimizing the code.


clearHrInterval

Clear the specified timer

1
static timers.clearHrInterval(Value t);

Call parameters:

  • t : Value, specify the timer to be cleared

setImmediate

Execute the callback function immediately in the next free 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 result:

  • Timer , returns the timer object

clearImmediate

Clear the specified timer

1
static timers.clearImmediate(Value t);

Call parameters:

  • t : Value, specify the timer to be cleared

call

Call the given function, and interrupt the function running when the timeout period expires

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

Call parameters:

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

Return result:

  • Value , returns the running result of func