Module global
Global objects, base objects accessible to all scripts
Object
Buffer
Binary data cache object for io Read and write data processing, see Buffer Object.
1Buffer global.Buffer;
console
Console access object
1console global.console;
process
Process object
1process global.process;
Static function
run
Run a script
1
2static global.run(String fname,
Array argv = []);
Call parameters:
- fname: String, specify the script path to run
- argv: Array, specify the parameter to be run, this parameter can be obtained using argv in the script
require
Load a module and return the module object, see @ref module for more information
1static Value global.require(String id);
Call parameters:
- id: String, specify the name of the module to be loaded
Return result:
- Value, Returns the exported object of the loaded module
require can be used to load basic modules and file modules.
The basic module is the module initialized when the sandbox is created. You only need to pass the corresponding id when referencing, such as require("net").
The file module is a user-defined module, and a relative path beginning with ./ or ../ must be passed when quoting. The file module supports .js, .jsc and.json document.
The file module also supports the package.json format. When the module has a directory structure, require will first query the main in package.json, and if not found, it will try to load index.js, index.jsc or index.json in the path.
If the reference path does not start with ./ or ../ and is not a basic module, require searches for node_modules under the path where the current module is located, and recursively from the upper-level directory.
The basic process is as follows:
setTimeout
Call the function after the specified time
1
2
3static Timer global.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 result:
- Timer, Returns the timer object
clearTimeout
Clear the specified timer
1static global.clearTimeout(Value t);
Call parameters:
- t: Value, specify the timer to be cleared
setInterval
Call the function after every specified time interval
1
2
3static Timer global.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, it will be executed immediately.
- args: ..., additional parameters, passed into the specified callback, optional.
Return result:
- Timer, Returns the timer object
clearInterval
Clear the specified timer
1static global.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
3static Timer global.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, it will be executed 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
10var 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 due to 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
1static global.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
2static Timer global.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
1static global.clearImmediate(Value t);
Call parameters:
- t: Value, specify the timer to be cleared
GC
Mandatory garbage collection
1static global.GC();
Static properties
Master
Worker, Worker Host object, only in Worker Entry script is valid
1static readonly Worker global.Master;
global
Object, global object
1static readonly Object new global;
argv
Array, get the running parameters of the current script, start js to get the process startup parameters, the script executed by run gets the passed parameters
1static readonly Array global.argv;
__filename
String, current script file name
1static readonly String global.__filename;
__dirname
String, the directory where the current script is located
1static readonly String global.__dirname;