_.defer(func, [args])
102
_.defer(func, [args])
_.defer延迟调用func函数直到当前的调用栈被清空。任何额外的参数都会在调用的时候传递给func函数
参数
func (Function): 需要延迟执行的函数
[args] (...*): 需要传递给func的额外参数
返回值
(number): 返回定时器id
例子
_.defer(function(text) { console.log(text); }, 'deferred'); // => Logs 'deferred' after one millisecond.
源代码:
/** * Defers invoking the `func` until the current call stack has cleared. Any * additional arguments are provided to `func` when it's invoked. * * @since 0.1.0 * @category Function * @param {Function} func The function to defer. * @param {...*} [args] The arguments to invoke `func` with. * @returns {number} Returns the timer id. * @example * * defer(text => console.log(text), 'deferred') * // => Logs 'deferred' after one millisecond. */ //延迟调用func函数直到当前的调用栈被清空。任何额外的参数都会在调用的时候传递给func函数 function defer(func, ...args) { if (typeof func != 'function') {//如果func不是函数,抛出错误 throw new TypeError('Expected a function') } return setTimeout(func, 1, ...args)//传递的额外参数会在调用的时候传递给func } export default defer