123456789101112131415161718192021222324252627282930313233 |
- // 节流
- export function throttle(fn, wait = 200) {
- let last, timer, now;
- return function() {
- now = Date.now();
- if (last && now - last < wait) {
- clearTimeout(timer);
- timer = setTimeout(function() {
- last = now;
- fn.call(this, ...arguments);
- }, wait);
- } else {
- last = now;
- fn.call(this, ...arguments);
- }
- };
- }
- // 防抖
- export function debounce(fn, wait = 200) {
- let timer;
- return function() {
- let context = this;
- let args = arguments;
- if (timer) clearTimeout(timer);
- timer = setTimeout(() => {
- fn.apply(context, args);
- }, wait)
- }
- }
- export default {
- throttle,
- debounce
- }
|