gpt4 book ai didi

javascript - 为什么 Underscore.js 有延迟功能?

转载 作者:可可西里 更新时间:2023-11-01 01:20:24 24 4
gpt4 key购买 nike

这是 Underscore.js 的 delay 函数的源代码:

_.delay = function (func, wait) {
var args = slice.call(arguments, 2);
return setTimeout(function () { return func.apply(null, args); }, wait);
};

这与 setTimeout 有何不同?为什么 Underscore.js 需要 delay

最佳答案

这是一种跨浏览器的方式,能够传递额外的参数,这些参数将作为回调的参数出现,例如 setTimeout()。这在 IE 中不起作用。

它可以让你的代码更漂亮......

setTimeout(_.bind(function() { }, null, "arg1"), 1e3);

...与...

_.delay(function() { }, 1e3, "arg1");

我同意它是不太有用的 Underscore 方法之一,在 Naomi's answer 中有概述。 .

关于javascript - 为什么 Underscore.js 有延迟功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16890106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com