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);
  };
};
相关推荐
爱勇宝8 小时前
我做了一个排版器,也踩了一遍富文本复制的坑
前端·后端·微信
铁皮饭盒9 小时前
面试官:如何用 Bun + JS 实现安全的文件 MCP 工具集
前端·javascript·后端
探索前端9 小时前
Cesium图层加载及影像服务添加
前端·cesium
阿懂在掘金9 小时前
Vue 弹窗新范式——代码减少、复用翻倍与 AI 时代的前端基建
前端·设计模式·前端框架
千纸鹤安安9 小时前
Compose Runtime → KuiklyUI:Applier 搭起的渲染桥梁
前端
李明卫杭州9 小时前
mise 管理工具详解
前端·后端
颜进强9 小时前
从零搭建私人 RAG 实战:让 Claude Code 按需查询知识库
前端·后端·ai编程
程序员黑豆9 小时前
鸿蒙应用开发:Scroll 组件从入门到实战
前端·华为·harmonyos
张人玉9 小时前
基于 Vue 3 + ECharts + Express + SQLite 构建的新能源汽车销量数据分析与可视化平台——新能源汽车销量数据分析系统
数据库·vue.js·sqlite·echarts
a11177610 小时前
三色软糖坠落玻璃池 THreeJS kimi
前端·人工智能·threejs