这篇文章也是衔接我以前文章,输入内容延迟显示。html
通常防抖函数,通常都是本身写,或者直接搜的相似这种vue
function debounce(fn,wait){ var timer = null; return function(){ if(timer !== null){ clearTimeout(timer); } timer = setTimeout(fn,wait); } }
https://cn.vuejs.org/v2/guide...git
我看到Vue官网 侦听器 使用了lodash
这个组件github
created: function () { // _.debounce 是一个经过 Lodash 限制操做频率的函数。 // 在这个例子中,咱们但愿限制访问 yesno.wtf/api 的频率 // AJAX 请求直到用户输入完毕才会发出。想要了解更多关于 // _.debounce 函数 (及其近亲 _.throttle) 的知识, // 请参考:https://lodash.com/docs#debounce this.debouncedGetAnswer = \_.debounce(this.getAnswer, 500) }
我就在想既然,官网都不用本身手写的,那我也用一下这个。npm
先看 https://lodash.com/docs#debounce 文档api
因为我只用了防抖,因此我只安装防抖函数ide
npm i --save lodash.debounce
使用函数
import debounce from 'lodash.debounce' textChange: debounce(function() { //注意这里若是使用箭头函数的话, this为 undefined https://github.com/vue-styleguidist/vue-docgen-api/issues/23 // do sth }, 300)
已经有轮子的话,就不要本身造轮子,固然练习的时候能够本身造。ui