vue封装全局的防抖节流函数

1.在入口文件main.js中:

//防抖函数
export let debounce = (() => {
  let instances = []
  return function (fun, delay = 300) {
    //项目中,多个地方用到防抖,进行判断,每次调用全局的防抖函数,是否是同一块代码
    const stackLines = new Error().stack.split('\n');
    let callerLine = stackLines[2];
    
    //判断数组中是否有了
    let index = instances.findIndex(item => {
      return item.callerLine == callerLine
    })
    if (index < 0) {
      //没有的话,直接新增一个
      instances.push({ callerLine, fun, delay, timer: null })
      index = instances.length - 1
    } else {
      //有了的话,直接将传递的逻辑函数赋值进去
      instances[index].fun = fun
    }

    //找到数据中对应的对象
    let instance = instances[index]
    let ctx = this
    let args = arguments
    //有时间间隔器,则清除
    if (instance.timer) {
      clearTimeout(instance.timer)
    }
    //进行其他逻辑
    instance.timer = setTimeout(() => {
      instance.timer = null
      instance.fun.apply(ctx, args)
    }, instance.delay)
  }
})()

//节流函数
export let throttle = (() => {
  let instances = []
  return function (fun, delay = 300) {
    const stackLines = new Error().stack.split('\n');
    let callerLine = stackLines[2];
    let index = instances.findIndex(item => {
      return item.callerLine == callerLine
    })
    if (index < 0) {
      instances.push({ callerLine, fun, delay, timeout: null })
      index = instances.length - 1
    } else {
      instances[index].fun = fun
    }
    let instance = instances[index]
    let args = arguments;//注意如果要传参的话 这句不能省略
    if (!instance.timeout) {
      instance.timeout = setTimeout(() => {
        instance.timeout = null;
        instance.fun.apply(this, args)
      }, instance.delay)
    }
  }
})()

Vue.prototype.$debounce = debounce;
Vue.prototype.$throttle = throttle;

2.页面中使用:

<div id="watch-example">
            <p>
                问一个是否的问题:
                <input v-model="question2">
            </p>
            <p>{{ answer2 }}</p>
        </div>
data() {
        return {
            question2: '',
            answer2: '请输入问题,否则我无法回答你!'
        };
    },

 

watch: {
        question2: function (newQuestion, oldQuestion) {
            this.answer2 = '请等待!';
            this.$debounce(this.getAnswer2, 1000);
        }
    },

 

getAnswer2: function() {
            if (this.question.indexOf('?') === -1) {
                this.answer2 = '请输入疑问句!';
                return;
            }else{
                this.answer2 = '请稍后...';
            setTimeout(() => {
                this.answer2 = '章总';
            }, 1000)
            }
            
        }

相关推荐

  1. vue封装全局节流函数

    2024-06-14 03:52:02       8 阅读
  2. Vue:封装响应式数据函数

    2024-06-14 03:52:02       18 阅读
  3. 函数,节流函数

    2024-06-14 03:52:02       34 阅读
  4. vue 函数

    2024-06-14 03:52:02       16 阅读
  5. 节流Vue和React当中节流处理

    2024-06-14 03:52:02       30 阅读
  6. vue3函数封装与使用,以指令形式使用

    2024-06-14 03:52:02       35 阅读
  7. 节流Vue优化技巧

    2024-06-14 03:52:02       51 阅读
  8. 手写节流函数

    2024-06-14 03:52:02       9 阅读
  9. vue中使用lodashdebounce函数

    2024-06-14 03:52:02       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-14 03:52:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-14 03:52:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-14 03:52:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-14 03:52:02       20 阅读

热门阅读

  1. 用Python编写自动发送每日电子邮件报告的脚本

    2024-06-14 03:52:02       6 阅读
  2. SuntoryProgrammingContest2024(AtCoder Beginner Contest 357)

    2024-06-14 03:52:02       8 阅读
  3. trpc快速上手

    2024-06-14 03:52:02       9 阅读
  4. kotlin 中的字符

    2024-06-14 03:52:02       10 阅读
  5. 计算机网络

    2024-06-14 03:52:02       7 阅读
  6. 【动态规划算法题记录】 509. 斐波那契数

    2024-06-14 03:52:02       7 阅读