當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。