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);
  };
};
相关推荐
独自破碎E21 小时前
【滑动窗口】BISHI47 交换到最大
java·开发语言·javascript
剑亦未配妥21 小时前
CSS 折叠引发的 scrollHeight 异常 —— 一次 Blink 引擎的诡异 Bug
前端·css·bug
CappuccinoRose21 小时前
HTML语法学习文档(三)
前端·学习·html·html5·标签·实体字符
0思必得021 小时前
[Web自动化] Selenium获取网页元素在桌面上的位置
前端·python·selenium·自动化
匀泪21 小时前
云原生(企业高性能 Web 服务器(Nginx 核心))
服务器·前端·云原生
国产化创客21 小时前
ESP32平台嵌入式Web前端框架选型分析
前端·物联网·前端框架·智能家居
是欢欢啊21 小时前
全新的table组件,vue3+element Plus
前端·javascript·vue.js
硬汉嵌入式1 天前
QEMU & FFmpeg作者Fabrice Bellard推出MicroQuickJS,一款面向嵌入式系统JavaScript引擎,仅需10K RAM
javascript·ffmpeg·microquickjs
GitCode官方1 天前
DevUI 组织 2025 年度运营报告:扎根 AtomGit,开源前端再启新程
前端·开源·atomgit