📜  Lodash _.now() 方法

📅  最后修改于: 2022-05-13 01:56:45.472000             🧑  作者: Mango

Lodash _.now() 方法

_.now() 方法用于获取自 Unix 纪元(1970 年 1 月 1 日 00:00:00 UTC)以来经过的毫秒数的时间戳。

句法:

_.now()

参数:此方法不接受任何参数。

返回值:此方法返回时间戳。

示例 1:

Javascript
// Defining Lodash variable
const _ = require('lodash');
  
// Getting the timestamp
console.log(_.now());


Javascript
// Defining Lodash variable
const _ = require('lodash');
  
// Creating print function to print
// timestamp after delay of 10ms 
function print(){ 
    setTimeout(() => { 
        console.log(
`Current timestamp is: ${_.now()}`) 
    }, 10) 
} 
  
// Running this function 5 times
// using _.times() function. 
_.times(5, print) 
console.log("Type of timestamp is: ", 
    typeof(_.now()))


输出:

1599545119753

示例 2:

Javascript

// Defining Lodash variable
const _ = require('lodash');
  
// Creating print function to print
// timestamp after delay of 10ms 
function print(){ 
    setTimeout(() => { 
        console.log(
`Current timestamp is: ${_.now()}`) 
    }, 10) 
} 
  
// Running this function 5 times
// using _.times() function. 
_.times(5, print) 
console.log("Type of timestamp is: ", 
    typeof(_.now())) 

输出:

Type of timestamp is:  number
Current timestamp is: 1599546710915
Current timestamp is: 1599546710916
Current timestamp is: 1599546710916
Current timestamp is: 1599546710916
Current timestamp is: 1599546710916

注意:这在普通 JavaScript 中不起作用,因为它需要安装 lodash 库。