• vue节流防抖-代码


    防抖:字面意思,怕多次提交,定时(定时2S 中调用后,重新刷新定时器 2S)
    节流:字面意思,防止提交频率过快,但还是要提交,定时(每2秒提交提交一次,中间不管你点了多少次 。间隔时间 = 延迟的时间 - (结束时间戳 - 开始时间戳)   )

     一、js方式

    1. export default {
    2. // 防抖
    3. debounce: function (fn, delay) {
    4. // 时间期限
    5. var delays = delay || 200
    6. var timer
    7. // 闭包
    8. return function () {
    9. // 考虑作用域,上下文环境,apply需要用到this对象
    10. var th = this
    11. // 接收的参数用 ES6 中的 rest 参数统一存储到变量 args 中。arguments就是传入的参数数组,而且个数可以不确定的传回给fn(不确定函数到底有多少个参数,用arguments来接收)
    12. var args = arguments
    13. // 判断还在定时,说明当前正在一个计时过程中,并且又触发了相同事件。所以要取消当前的计时,重新开始计时
    14. if (timer) {
    15. clearTimeout(timer)
    16. }
    17. timer = setTimeout(function () {
    18. timer = null
    19. // 执行方法
    20. fn.apply(th, args)
    21. }, delays)
    22. }
    23. },
    24. // 节流 定时器 + 时间戳
    25. throttle: function(func, delay) {
    26. var timer = null
    27. var startTime = Date.now()
    28. return function() {
    29. // 结束时间
    30. var curTime = Date.now()
    31. // 间隔时间 = 延迟的时间 - (结束时间戳 - 开始时间戳)
    32. var interval = delay - (curTime - startTime)
    33. var th = this
    34. var args = arguments
    35. clearTimeout(timer)
    36. if (interval <= 0) {
    37. // 证明可以触发了
    38. func.apply(th, args)
    39. // 重新计算开始时间
    40. startTime = Date.now()
    41. } else {
    42. // 继续等待
    43. timer = setTimeout(func, interval)
    44. }
    45. }
    46. }
    47. }

    import antiShake from '@/utils/antiShake.js' // 防抖节流
     

    1. methods: {
    2. refresh: antiShake.throttle(function () {
    3. // 需要防抖的内容
    4. that.fetch()
    5. }, 2000),
    6. // 关闭操作
    7. close() {
    8. if (this.visible) {
    9. this.visible = false
    10. this.$refs.form.resetFields()
    11. }
    12. if (this.sampleVisible) {
    13. this.sampleVisible = false
    14. this.$refs.sampleVisible.resetFields()
    15. }
    16. },

    that.fetch() 用this会报错,但是能使用
    全局定义个that
     

    1. created() {
    2. that = this
    3. this.fetch()
    4. },

    二、指令
    定义

    1. Vue.directive('debounce',{
    2. inserted:(el,binding)=>{
    3. let delay= binding.args
    4. if(!delay){
    5. delay=1000
    6. }
    7. let time
    8. el.addEventListener('input',()=>{
    9. if(time){
    10. clearTimeout(time)
    11. }
    12. time= setTimeout(()=>{
    13. binding.value()
    14. },delay)
    15. })
    16. }
    17. })
    1. Vue.directive('throttle',{
    2. inserted:(el,binding)=>{
    3. let delay =binding.args
    4. if(!delay) delay=1000
    5. let time =null
    6. el.addEventListener('input',()=>{
    7. if(!time){
    8. time=setTimeout(()=>{
    9. binding.value()
    10. time=null
    11. },delay)
    12. }
    13. })
    14. }
    15. })

    使用

    1. <template>
    2. <button v-debounce:1000="debounceClick">防抖</button>
    3. </template>
    4. <script>
    5. export default {
    6. methods: {
    7. debounceClick () {
    8. console.log('只触发一次')
    9. }
    10. }
    11. }
    12. </script>
    1. <template>
    2. <button v-throttle:1000="debounceClick">防抖</button>
    3. </template>
    4. <script>
    5. export default {
    6. methods: {
    7. debounceClick () {
    8. console.log('只触发一次')
    9. }
    10. }
    11. }
    12. </script>

    三、插件 

    npm install throttle-debounce --save
    import { throttle } from ‘throttle-debounce’;
     

    1. throttleCallback: throttle(1000, false, () => {
    2. //业务
    3. }),
    4. debounceCallback: debounce(1000, false, () => {
    5. //业务
    6. }),

  • 相关阅读:
    《无与伦比》Centos7 安装docker-compose
    order by注入与limit注入
    xxl-job 2.2之后版本高版本executor未授权访问漏洞
    SQL SELECT DISTINCT(选择不同) 语法
    高项 08 人力资源管理
    java缓存
    【从零开始学习 SystemVerilog】8.4、SystemVerilog 约束—— Array Randomization(数组随机化)
    无法删除dll文件
    Verilog实现定点乘法器
    学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
  • 原文地址:https://blog.csdn.net/weixin_41127362/article/details/126608193