简体   繁体   English

去抖动的函数调用已延迟,但全部在等待计时器结束时执行

[英]Debounced function calls delayed but all executed when wait timer ends

I am using debounce in order to implement a 'search as you type' field. 我正在使用反跳以便实现“键入时搜索”字段。

I am reading https://css-tricks.com/debouncing-throttling-explained-examples/ and from what I can understand the function should only be called a limited number of times. 我正在阅读https://css-tricks.com/debouncing-throttling-explained-examples/ ,据我了解,该函数只能调用有限次。

In my case, the function calls are delayed, but executed all at once once the wait timer ends: 就我而言,函数调用是延迟的,但是一旦等待计时器结束就立即全部执行:

methods: {
  searchOnType: function (currentPage, searchString) {
    console.log(`Searching ${searchString}`)
    var debounced = throttle(this.getMovies, 4000, {leading: false, trailing: true})
    debounced('movies.json', currentPage, searchString)
  },
  getMovies: function (url, page, query) {
    console.log(query)
    this.loading = true
    resourceService.getMovies(url, page, query).then((result) => {
      this.items = result.movies
      this.totalMovies = result.total
      this.loading = false
    })
  },

the HTML (it's Vue.JS) HTML(是Vue.JS)

  <input 
    type="text" 
    v-model="searchString" 
    class="form-control input-lg" 
    placeholder="search movie" 
    @keydown="searchOnType(currentPage, searchString)"
  >

This is my console.log: 这是我的console.log: 安慰

You're creating a throttled function every time you keydown (you should probably be using input instead by the way). 每次keydown都在创建一个受限制的函数(顺便说一句,您可能应该使用input )。 You can do this instead I think... 您可以改为这样做,我认为...

methods: {
  searchOnType: throttle((currentPage, searchString) => {
    this.getMovies('movies.json', currentPage, searchString)
  }, 1000, {leading: false, trailing: true})
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM