当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js process.uptime()用法及代码示例


process.uptime()方法是进程模块的内置应用程序编程接口,用于获取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




相关用法


注:本文由纯净天空筛选整理自gekcho大神的英文原创作品 Node.js | process.uptime() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。