| 1234567891011121314151617181920212223242526272829303132 |
- /**
- * @desc 函数节流
- * @param func 函数
- * @param wait 延迟执行毫秒数
- * @param type 1 表时间戳版,2 表定时器版
- */
- export const throttle = (func, wait, type) => {
- if (type === 1) {
- var previous = 0;
- } else if (type === 2) {
- var timeout;
- }
- return function() {
- let context = this;
- let args = arguments;
- if (type === 1) {
- let now = Date.now();
- if (now - previous > wait) {
- func.apply(context, args);
- previous = now;
- }
- } else if (type === 2) {
- if (!timeout) {
- timeout = setTimeout(() => {
- timeout = null;
- func.apply(context, args);
- }, wait);
- }
- }
- };
- };
|