vue ts 本地缓存数据
需求是:给每日最高热度的数据,每个用户弹窗三次,持续五秒
const popupKey = 'dailyPopupCount_';
const today = new Date().toISOString().split('T')[0];
const popupCount = ref(parseInt(localStorage.getItem(`${popupKey}${today}`) || '0', 10));
export const showHost = ref(false);
export const countdown = ref(0);
export let timer: number | null | any = null;
export const checkAndShowPopup = () => {if (popupCount.value < 3) {showHost.value = true;countdown.value = 5; timer = setInterval(updateCountdown, 1000); popupCount.value++;localStorage.setItem(`${popupKey}${today}`, popupCount.value.toString());}for (let i = localStorage.length - 1; i >= 0; i--) {const key = localStorage.key(i);if (key) {if (key.startsWith(popupKey) && !key.endsWith(today)) {localStorage.removeItem(key);}}}
};
export const hidePopup = () => {showHost.value = false;if (timer) {timeFun();clearTime();}
};const timeFun = () => {showHost.value = false;localStorage.setItem(`${popupKey}${today}`, popupCount.value.toString());
};const clearTime = () => {clearInterval(timer!);timer = null;
};
const updateCountdown = () => {if (countdown.value > 0) {countdown.value--;} else {timeFun();clearTime();}
};