![](/img/trans.png)
[英]How to pass additional parameters to an encapsulated callback function in node.js
[英]How to pass unknown additional parameters to callback function when invoked?
等待毫秒后调用func。 调用func时,会提供任何其他参数 。
我想不出一种将未定义数量的附加参数传递给回调函数的好方法。 有什么建议吗?
function delay(func, wait) {
return setTimeout(func, wait);
}
// func will run after wait millisec delay
// Example
delay(hello, 100);
delay(hello, 100, 'joe', 'mary'); // 'joe' and 'mary' will be passed to hello function
这样定义延迟
function delay(fn, ms) {
var args = [].slice.call(arguments, 2);
return setTimeout(function() {
func.apply(Object.create(null), args);
}, ms);
}
或者,如果您是ES6 / 7风扇
function delay(fn, ms, ...args) {
return setTimeout(function() {
func.apply(Object.create(null), args);
}, ms);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.