12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- export const formatTime = (date: Date) => {
- const year = date.getFullYear()
- const month = date.getMonth() + 1
- const day = date.getDate()
- const hour = date.getHours()
- const minute = date.getMinutes()
- const second = date.getSeconds()
- return (
- [year, month, day].map(formatNumber).join('/') +
- ' ' +
- [hour, minute, second].map(formatNumber).join(':')
- )
- }
- const formatNumber = (n: number) => {
- const s = n.toString()
- return s[1] ? s : '0' + s
- }
- /**
- * 函数防抖: 当事件被触发 n 秒后再执行回调,如果在 n 秒内又被触发,则重新计时。
- * @param {*} func
- * @param {*} wait
- * @returns { Function }
- */
- export const debounce = (func: any, wait: number) => {
- let delay = wait || 500;
- let timer: number | null;
- return () => {
- const _this = this;
- let args: any = arguments;
- if(timer) clearTimeout(timer);
- timer = setTimeout(() => {
- timer = null;
- func.apply(_this, args)
- }, delay)
- }
- }
- /**
- * 函数节流: 规定在一个单位时间内只能触发一次函数,如果在单位时间内触发多次,只执行一次。
- * @param {*} func
- * @param {*} wait
- * @returns { Function }
- */
- export const throttle = (func: { apply: (arg0: undefined, arg1: any) => void }, wait: number) => {
- let delay = wait || 500;
- let timer: number | null;
- return () => {
- let _this = this;
- let args = arguments;
- if(!timer) {
- timer = setTimeout(() => {
- func.apply(_this, args);
- timer = null;
- }, delay)
- }
- }
- }
|