Lodash - delay 方法

语法

_.delay(func, wait, [args])

等待几毫秒后调用 func。调用时会向 func 提供任何其他参数。

参数

  • func (Function) − 要延迟的函数。

  • wait (number) − 延迟调用的毫秒数。

  • [args] (...*) −调用 func 的参数。

输出

  • (number) − 返回计时器 ID。

示例

var _ = require('lodash');
var startTimestamp = new Date().getTime();

var add = function(a,b) {
    console.log(a + b);
    var endTimestamp = new Date().getTime();
    console.log(((endTimestamp - startTimestamp)) + ' ms');
};
_.delay(add, 1000, 5, 10);

将上述程序保存在 tester.js 中。运行以下命令来执行该程序。

命令

\>node tester.js

输出

15
1024 ms

lodash_function.html