import toNumber from './toNumber.js' /** * Invokes `func` after `wait` milliseconds. Any additional arguments are * provided to `func` when it's invoked. * * @since 0.1.0 * @category Function * @param {Function} func The function to delay. * @param {number} wait The number of milliseconds to delay invocation. * @param {...*} [args] The arguments to invoke `func` with. * @returns {number} Returns the timer id. * @example * * delay(text => console.log(text), 1000, 'later') * // => Logs 'later' after one second. */ function delay(func, wait, ...args) { if (typeof func != 'function') { throw new TypeError('Expected a function') } return setTimeout(func, toNumber(wait) || 0, ...args) } export default delay
我把源码贴出来了,其实就是做了一层封装,本质上没什么区别,但是加了错误处理,并提供了一些附加解决方法,提高了容错率吧