Files
frontend-app/utils/func.js
T
2025-09-12 17:56:35 +08:00

64 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
let timeout = null;
let timer;
let flag;
export default {
formatDate (date,val) {
let y = date.getFullYear();
let m = date.getMonth()+1;
let d = date.getDate();
let h = date.getHours();
let mm = date.getMinutes();
let s = date.getSeconds();
if(m <= 9){
m = '0' + m;
}
if(d <= 9){
d = '0' + d;
}
if(h <= 9){
h = '0' + h;
}
if(mm <= 9){
mm = '0' + mm;
}
if(s <= 9){
s = '0' + s;
}
let tmpTime = '';
if(val == 0){
tmpTime = `${h}:${mm}`;
}else if(val == 1){
tmpTime = `${m}-${d}`;
}else if(val == 2){
tmpTime = `${y}-${m}-${d} ${h}:${mm}:${s}`;
}else if(val == 3){
tmpTime = `${y}-${m}-${d}`;
}
return tmpTime;
}, 
debounce(func,wait = 1000,immediate = true){
// 清除定时器
if (timeout !== null) clearTimeout(timeout)
// 立即执行,此类情况一般用不到
if (immediate) {
const callNow = !timeout
timeout = setTimeout(() => {
timeout = null
}, wait)
if (callNow) typeof func === 'function' && func()
} else {
// 设置定时器,当最后一次操作后,timeout不会再被清除,所以在延时wait毫秒后执行func回调方法
timeout = setTimeout(() => {
typeof func === 'function' && func()
}, wait)
}
}
}