@ceale/util 1.7.1 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +33 -0
- package/dist/esm/index.js +33 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/task.d.ts +20 -0
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -32,8 +32,10 @@ __export(exports_src, {
|
|
|
32
32
|
waitSync: () => waitSync,
|
|
33
33
|
wait: () => wait,
|
|
34
34
|
uri: () => uri,
|
|
35
|
+
throttle: () => throttle,
|
|
35
36
|
sleepAsync: () => sleepAsync,
|
|
36
37
|
sleep: () => sleep,
|
|
38
|
+
debounce: () => debounce,
|
|
37
39
|
css: () => css
|
|
38
40
|
});
|
|
39
41
|
module.exports = __toCommonJS(exports_src);
|
|
@@ -216,3 +218,34 @@ var sleep = (time) => {
|
|
|
216
218
|
return;
|
|
217
219
|
};
|
|
218
220
|
var waitSync = sleep;
|
|
221
|
+
// src/task.ts
|
|
222
|
+
var debounce = (func, delay) => {
|
|
223
|
+
let timer = null;
|
|
224
|
+
return function(...args) {
|
|
225
|
+
if (timer) {
|
|
226
|
+
clearTimeout(timer);
|
|
227
|
+
}
|
|
228
|
+
timer = setTimeout(() => {
|
|
229
|
+
func.apply(this, args);
|
|
230
|
+
}, delay);
|
|
231
|
+
};
|
|
232
|
+
};
|
|
233
|
+
var throttle = (func, delay) => {
|
|
234
|
+
let timer = false;
|
|
235
|
+
let lastArgs = null;
|
|
236
|
+
return function(...args) {
|
|
237
|
+
if (timer) {
|
|
238
|
+
lastArgs = args;
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
func.apply(this, args);
|
|
242
|
+
timer = true;
|
|
243
|
+
setTimeout(() => {
|
|
244
|
+
if (lastArgs) {
|
|
245
|
+
func.apply(this, lastArgs);
|
|
246
|
+
lastArgs = null;
|
|
247
|
+
}
|
|
248
|
+
timer = false;
|
|
249
|
+
}, delay);
|
|
250
|
+
};
|
|
251
|
+
};
|
package/dist/esm/index.js
CHANGED
|
@@ -176,11 +176,44 @@ var sleep = (time) => {
|
|
|
176
176
|
return;
|
|
177
177
|
};
|
|
178
178
|
var waitSync = sleep;
|
|
179
|
+
// src/task.ts
|
|
180
|
+
var debounce = (func, delay) => {
|
|
181
|
+
let timer = null;
|
|
182
|
+
return function(...args) {
|
|
183
|
+
if (timer) {
|
|
184
|
+
clearTimeout(timer);
|
|
185
|
+
}
|
|
186
|
+
timer = setTimeout(() => {
|
|
187
|
+
func.apply(this, args);
|
|
188
|
+
}, delay);
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
var throttle = (func, delay) => {
|
|
192
|
+
let timer = false;
|
|
193
|
+
let lastArgs = null;
|
|
194
|
+
return function(...args) {
|
|
195
|
+
if (timer) {
|
|
196
|
+
lastArgs = args;
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
func.apply(this, args);
|
|
200
|
+
timer = true;
|
|
201
|
+
setTimeout(() => {
|
|
202
|
+
if (lastArgs) {
|
|
203
|
+
func.apply(this, lastArgs);
|
|
204
|
+
lastArgs = null;
|
|
205
|
+
}
|
|
206
|
+
timer = false;
|
|
207
|
+
}, delay);
|
|
208
|
+
};
|
|
209
|
+
};
|
|
179
210
|
export {
|
|
180
211
|
waitSync,
|
|
181
212
|
wait,
|
|
182
213
|
uri,
|
|
214
|
+
throttle,
|
|
183
215
|
sleepAsync,
|
|
184
216
|
sleep,
|
|
217
|
+
debounce,
|
|
185
218
|
css
|
|
186
219
|
};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 将传入的函数变为防抖版本
|
|
3
|
+
*
|
|
4
|
+
* 在间隔低于`delay`连续触发函数时,只会执行最后一次调用,并在最后一次调用后延时`delay`执行
|
|
5
|
+
* @param func 原始函数
|
|
6
|
+
* @param delay 防抖时长,毫秒
|
|
7
|
+
* @returns 带防抖的函数
|
|
8
|
+
*/
|
|
9
|
+
export declare const debounce: <F extends (...args: any[]) => void>(func: F, delay: number) => ((...args: Parameters<F>) => void);
|
|
10
|
+
/**
|
|
11
|
+
* 将传入的函数变为节流版本
|
|
12
|
+
*
|
|
13
|
+
* 函数将在首次调用后执行,首次调用后的`delay`内,如重复调用,
|
|
14
|
+
*
|
|
15
|
+
* 则函数将以最后一次调用的参数,在首次调用后延迟`delay`后执行
|
|
16
|
+
* @param func 原始函数
|
|
17
|
+
* @param delay 节流时长,毫秒
|
|
18
|
+
* @returns 带节流的函数
|
|
19
|
+
*/
|
|
20
|
+
export declare const throttle: <F extends (...args: any[]) => void>(func: F, delay: number) => ((...args: Parameters<F>) => void);
|