javascript 去抖动功能不会在vue 3中启动

vdgimpew  于 5个月前  发布在  Java
关注(0)|答案(1)|浏览(59)

我一直在使用composition API在vue 3的watch函数中触发debounce函数时遇到麻烦。我让它在选项API中工作。watch函数正在工作(只是不是watcher内部的debounce函数)。

setup() {
  const handleDebounce = (fn, delay) => {
    console.log('test') // this works
    var timeoutID = null
    return function () {
      console.log('test') // this does not work
      clearTimeout(timeoutID)
      var args = arguments
      var that = this
      timeoutID = setTimeout(function () {
        fn.apply(that, args)
      }, delay)
    }
  }

  watch(
    () => watched.value,
    input => {
      //doesn't work
      handleDebounce(function (t) {
        console.log(t)
      }, 3000)
    }
  )
}

字符串

k0pti3hp

k0pti3hp1#

或者这样写手表:

watch(
  () => watched.value,
  handleDebounce(function (input) {
    console.log(input)
  }, 3000)
)

字符串
或者在手表外部创建一个去抖动函数,然后在手表内部调用它。

const someLogging = handleDebounce(function (t) {
  console.log(t)
}, 3000)

watch(
  () => watched.value,
  input => {
    someLogging(input)
  }
)

相关问题