一、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);
};
};