Showing
1 changed file
with
64 additions
and
0 deletions
src/utils/common/compUtils.ts
0 → 100644
1 | +/** | |
2 | + * 简单实现防抖方法 | |
3 | + * | |
4 | + * 防抖(debounce)函数在第一次触发给定的函数时,不立即执行函数,而是给出一个期限值(delay),比如100ms。 | |
5 | + * 如果100ms内再次执行函数,就重新开始计时,直到计时结束后再真正执行函数。 | |
6 | + * 这样做的好处是如果短时间内大量触发同一事件,只会执行一次函数。 | |
7 | + * | |
8 | + * @param fn 要防抖的函数 | |
9 | + * @param delay 防抖的毫秒数 | |
10 | + * @returns {Function} | |
11 | + */ | |
12 | +export function simpleDebounce(fn, delay = 100) { | |
13 | + let timer: any | null = null; | |
14 | + return function () { | |
15 | + let args = arguments; | |
16 | + if (timer) { | |
17 | + clearTimeout(timer); | |
18 | + } | |
19 | + timer = setTimeout(() => { | |
20 | + // @ts-ignore | |
21 | + fn.apply(this, args); | |
22 | + }, delay); | |
23 | + }; | |
24 | +} | |
25 | + | |
26 | +// /** | |
27 | +// * 日期格式化 | |
28 | +// * @param date 日期 | |
29 | +// * @param block 格式化字符串 | |
30 | +// */ | |
31 | +export function dateFormat(date, block) { | |
32 | + if (!date) { | |
33 | + return ''; | |
34 | + } | |
35 | + let format = block || 'yyyy-MM-dd'; | |
36 | + date = new Date(date); | |
37 | + const map = { | |
38 | + M: date.getMonth() + 1, // 月份 | |
39 | + d: date.getDate(), // 日 | |
40 | + h: date.getHours(), // 小时 | |
41 | + m: date.getMinutes(), // 分 | |
42 | + s: date.getSeconds(), // 秒 | |
43 | + q: Math.floor((date.getMonth() + 3) / 3), // 季度 | |
44 | + S: date.getMilliseconds(), // 毫秒 | |
45 | + }; | |
46 | + format = format.replace(/([yMdhmsqS])+/g, (all, t) => { | |
47 | + let v = map[t]; | |
48 | + if (v !== undefined) { | |
49 | + if (all.length > 1) { | |
50 | + v = `0${v}`; | |
51 | + v = v.substr(v.length - 2); | |
52 | + } | |
53 | + return v; | |
54 | + } else if (t === 'y') { | |
55 | + return date | |
56 | + .getFullYear() | |
57 | + .toString() | |
58 | + .substr(4 - all.length); | |
59 | + } | |
60 | + return all; | |
61 | + }); | |
62 | + return format; | |
63 | +} | |
64 | + | ... | ... |