我需要在本地存盤中存盤和更新時間戳,因此一分鐘后,模式每次都會執行。我希望僅當時間戳已過期或超過一分鐘時,模態才會出現在重繪 時。一分鐘的持續時間僅用于測驗目的。目標是每天只向用戶顯示一次模式。這是我到目前為止所擁有的。它僅在重繪 時顯示模式并存盤本地存盤密鑰。但是一分鐘后,模式不會出現在重繪 并且應該出現。任何幫助表示贊賞。
$(document).ready(function() {
function shouldShowModal() {
const lastTimestamp = localStorage.getItem("modalTimestamp");
if (!lastTimestamp) {
return true;
}
const parsedTimestamp = parseInt(lastTimestamp, 10);
let expiryLimit = new Date(parsedTimestamp);
expiryLimit = expiryLimit.setMinutes(expiryLimit.getMinutes() 1);
return new Date() > expiryLimit;
}
if (window.localStorage) {
const currentUser = '<%= current_user %>';
const currentTime = new Date($.now());
if (currentUser && shouldShowModal()) {
swal ({
icon: 'warning',
text: 'Notice text here.',
closeOnClickOutside: false,
button: 'ok'
})
localStorage.setItem('modalTimestamp', currentTime);
}
}
}
更新了使用@Barmar 解決方案:
$(document).ready(function() {
function shouldShowModal() {
const lastTimestamp = localStorage.getItem("modalTimestamp");
if (!lastTimestamp) {
return true;
}
const parsedTimestamp = parseInt(lastTimestamp, 10);
let expiryLimit = new Date(parsedTimestamp);
expiryLimit = expiryLimit.setHours(expiryLimit.getHours() 24);
return new Date() > expiryLimit;
}
if (window.localStorage) {
const currentUser = '<%= current_user %>';
const currentTime = new Date();
if (currentUser && shouldShowModal()) {
swal ({
icon: 'warning',
text: 'Notice text here.',
closeOnClickOutside: false,
button: 'ok'
})
localStorage.setItem('modalTimestamp', currentTime.getTime());
}
}
}
uj5u.com熱心網友回復:
currentTime是一個Date物件,您需要將數字時間戳保存在本地存盤中。所以使用
localStorage.setItem('modalTimestamp', currentTime.getTime());
$.now()我也認為設定時不需要使用currentTime. new Date()默認回傳當前時間。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/493644.html
標籤:javascript jQuery 本地存储
下一篇:選擇當前懸停的div
