1 /**
2 * @param {Function} fn 防抖函数
3 * @param {Number} delay 延迟时间
4 */
5 export function debounce(fn, delay) {
6 var timer;
7 return function () {
8 var context = this;
9 var args = arguments;
10 clearTimeout(timer);
11 timer = setTimeout(function () {
12 fn.apply(context, args);
13 }, delay);
14 };
15 }
16
17 /**
18 * @param {date} time 需要转换的时间
19 * @param {String} fmt 需要转换的格式 如 yyyy-MM-dd、yyyy-MM-dd HH:mm:ss
20 */
21 export function formatTime(time, fmt) {
22 if (!time) return '';
23 else {
24 const date = new Date(time);
25 const o = {
26 'M+': date.getMonth() + 1,
27 'd+': date.getDate(),
28 'H+': date.getHours(),
29 'm+': date.getMinutes(),
30 's+': date.getSeconds(),
31 'q+': Math.floor((date.getMonth() + 3) / 3),
32 S: date.getMilliseconds(),
33 };
34 if (/(y+)/.test(fmt))
35 fmt = fmt.replace(
36 RegExp.$1,
37 (date.getFullYear() + '').substr(4 - RegExp.$1.length)
38 );
39 for (const k in o) {
40 if (new RegExp('(' + k + ')').test(fmt)) {
41 fmt = fmt.replace(
42 RegExp.$1,
43 RegExp.$1.length === 1
44 ? o[k]
45 : ('00' + o[k]).substr(('' + o[k]).length)
46 );
47 }
48 }
49 return fmt;
50 }
51 }