vue3 防抖函数传参

使用防抖函数传递参数时,参数会存在arguments中

防抖函数

/* 函数防抖 */
function debounce (fn, interval) {
  let timer
  const gapTime = interval || 1000 // 间隔时间,如果interval不传,则默认1000ms
  return function () {
    clearTimeout(timer)
    const context = this
    const args = arguments // 保存此处的arguments,因为setTimeout是全局的,arguments不是防抖函数需要的。
    timer = setTimeout(function () {
      fn.call(context, args)
    }, gapTime)
  }
}

使用

const choseCityByName = utils.debounce((data) => {
  console.log(data[0], data[1])
  // 参数item为date[0], 参数index为data[1]
}, 200)

传参

//参数会存储在防抖函数的arguments中
choseCityByName(item, index)

猜你喜欢

转载自blog.csdn.net/weixin_42215897/article/details/126578536