53 lines
1.4 KiB
JavaScript
53 lines
1.4 KiB
JavaScript
//格式化时间戳
|
||
export function formatDeadline(timestamp) {
|
||
const date = new Date(timestamp * 1000); // 时间戳以秒为单位,需要乘以1000转换成毫秒
|
||
const year = date.getFullYear();
|
||
const month = ("0" + (date.getMonth() + 1)).slice(-2);
|
||
const day = ("0" + date.getDate()).slice(-2);
|
||
const hours = ("0" + date.getHours()).slice(-2);
|
||
const minutes = ("0" + date.getMinutes()).slice(-2);
|
||
const seconds = ("0" + date.getSeconds()).slice(-2);
|
||
|
||
return `${year}年${month}月${day}日 ${hours}:${minutes}:${seconds}`;
|
||
}
|
||
|
||
// 防抖函数
|
||
export function debounce(fn, delay) {
|
||
let timer = null;
|
||
return (fnn) => {
|
||
//清除上一次的延时器
|
||
if (timer) {
|
||
clearTimeout(timer);
|
||
// return;
|
||
// console.log(timer);
|
||
}
|
||
//重新设置新的延时器
|
||
timer = setTimeout(() => {
|
||
//修改this指向问题
|
||
// fn.apply(this,value)
|
||
fn(fnn);
|
||
}, delay);
|
||
};
|
||
}
|
||
|
||
export function utf8Length(str) {
|
||
let length = 0;
|
||
for (let i = 0; i < str.length; i++) {
|
||
const code = str.charCodeAt(i);
|
||
if (code < 0x80) {
|
||
// 0xxxxxxx
|
||
length += 0.5;
|
||
} else if (code < 0x800) {
|
||
// 110xxxxx 10xxxxxx
|
||
length += 2;
|
||
} else if (code < 0x10000) {
|
||
// 1110xxxx 10xxxxxx 10xxxxxx
|
||
length += 1;
|
||
} else if (code <= 0x10ffff) {
|
||
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||
length += 4;
|
||
}
|
||
}
|
||
return length;
|
||
}
|