durations中文文档|durations js中文教程|解析
安装命令:npm i durations
durations
兼容性
Node.js 和浏览器都支持durations
。使用 Node.js 时,使用纳秒粒度process.hrtime()
函数。在浏览器中选择最佳替代,这样即使时间粒度不能保持一致性。
安装
npm install --save durations
方法
导出以下函数:
duration(nanoseconds)
– 构建一个新的持续时间nanos(nanoseconds)
– 构建一个新的持续时间micros(microseconds)
– 构建一个新的持续时间millis(milliseconds)
– 构建一个新的持续时间seconds(seconds)
– 构建一个新的持续时间stopwatch()
– 构建一个新的秒表(停止)time(function)
– 同步时间函数timeAsync(function(callback))
– 异步时间函数timePromised(function())
– 次承诺返回函数
期间
表示纳秒粒度的持续时间,并提供转换为其他粒度的方法,以及格式化持续时间的方法。
方法
format()
– 表示持续时间的人类可读字符串nanos()
– 持续时间为纳秒micros()
– 持续时间为微秒millis()
– 以毫秒为单位的持续时间seconds()
– 持续时间为秒minutes()
– 持续时间为分钟hours()
– 持续时间为小时days()
– 持续时间为天
const duration = const nanoseconds = 987654321console // Or, since toString() is an alias to format()console
跑表
具有可链接控制方法和内置格式的纳秒粒度(在 Node.js 上)秒表。
秒表方法
start()
– 启动并返回秒表(如果已经运行则无操作)stop()
– 停止并返回秒表(如果不运行则无操作)reset()
– 将经过时间重置为零并返回秒表(意味着停止)duration()
– 获取经过的时间作为持续时间isRunning()
– 正在运行的秒表 (true
/false
)
const stopwatch = const watch = // Pauses the stopwatch. Returns the stopwatch.watch // Starts the stopwatch from where it was last stopped. Returns the stopwatch.watchstart // Reset the stopwatch (duration is set back to zero). Returns the stopwatch.watch console// ORconsole
定时器
对函数的执行进行计时,并返回持续时间。
const time: timeSync timeAsync = // Synchronous workconst someFunction = { let count = 0 while count < 1000000 count++ console} console // Asynchronous workconst someOtherFunction = { } // Promised workconst somePromisedOp = { return { }}
项目issue数量: 0
项目贡献人员列表: