Node.js process.uptime() 方法
process.uptime() 方法是 process 模块的内置应用程序编程接口,用于获取 Node.js 进程运行的秒数。
句法:
process.uptime();
参数:此方法不接受任何参数。
返回值:它返回一个浮点数,指定 Node.js 进程运行的秒数。
下面的例子说明了在 Node.js 中 process.uptime() 方法的使用:
示例 1:
// Node.js program to demonstrate the
// process.uptime() Method
// Allocating process module
const process = require('process');
// Printing the number of seconds
// the Node.js process is running
console.log(process.uptime());
输出:
0.0842063
示例 2:
// Node.js program to demonstrate the
// process.uptime() Method
// Allocating process module
const process = require('process');
// Checking whether the method
// exists or not
if (process.uptime) {
// Printing uptime() value
console.log("The number of seconds the"
+ " Node.js process is running: "
+ process.uptime() + " seconds");
var i = 1000000;
while(i--);
console.log("The number of seconds the"
+ " Node.js process is running: "
+ process.uptime() + " seconds");
console.log("In whole seconds: "
+ Math.floor(process.uptime())
+ " seconds");
}
输出:
the number of seconds the Node.js process is running: 0.077 seconds
the number of seconds the Node.js process is running: 0.087 seconds
In whole seconds: 0 seconds
注意:上面的程序将使用node filename.js
命令编译和运行。
参考: https://nodejs.org/api/process.html#process_process_uptime