vue 防抖与节流用法

一、html

javascript 复制代码
<template>
    <button @click="getData">获取数据</button>
</template>

二、JS

javascript 复制代码
import { throttle } from "@/utils/common";
export default {
    methods:{
        getData: throttle(async function(params){
            console.log("获取接口数据",this,parmas)
        })
    }
}

三、公共方法 common.js

javascript 复制代码
// 节流
export const throttle = function (cb, delay) {
  let timer = null;
  return function (...arg) {
    if (timer) return;
    cb.call(this, arg[0]);
    timer = setTimeout(() => {
      timer = null;
    }, delay);
  };
};
javascript 复制代码
// 防抖
export const debounce = function (cb, delay) {
  let timer = null;
  return function (...arg) {
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => {
      cb.call(this, arg[0]);
    }, delay);
  };
};
相关推荐
anOnion15 小时前
构建无障碍组件之Switch Pattern
前端·html·交互设计
华洛15 小时前
多写点skill吧,写的越多这行业死的越快。
前端·javascript·产品
剪刀石头布啊15 小时前
从函数式编程介绍
前端
vjmap16 小时前
全新唯杰WebCAD编辑平台发布:全面拥抱AI,WebCAD智能体(Agent)来了
前端·gis·ai编程
剪刀石头布啊17 小时前
扫码登录方式
前端
剪刀石头布啊17 小时前
浏览器指纹
前端
剪刀石头布啊17 小时前
前端截图html2canvas
前端
IT_陈寒19 小时前
别再死记硬背Python语法了!这5个思维模式让你代码量减半
前端·人工智能·后端
beata19 小时前
Java基础-19:Java 死锁深度解析:从原理、检测到预防与实战指南
java·前端
Sunshine11119 小时前
浏览器渲染zz
前端