gpt4 book ai didi

javascript - 有没有更好的方法来设置和从 LocalStorage 获取?

转载 作者:可可西里 更新时间:2023-11-01 13:28:16 25 4
gpt4 key购买 nike

我正在尝试找出一种更好的方法来完成我下面的代码所做的事情..它所做的是在 localstorage 中保存一个时间戳,以了解最后一次单击该链接的时间,然后在时间用完时出现。但是如果我想添加更多链接,将会有很多复制粘贴......有人可以帮助我如何循环执行“保存和加载”脚本吗?也许在链接中而不是在脚本中添加要计算的分钟数?

我现在是这样做的:

<html>
<head>
<script type="text/javascript" src="script.js"></script>
</head>
<body onload="checkLocalStorage()">
<a onclick="saveTime01()" id="linkID01" href="#">Link1</a>
<a onclick="saveTime02()" id="linkID02" href="#">Link2</a>
<a onclick="saveTime03()" id="linkID03" href="#">Link3</a>
<a onclick="saveTime04()" id="linkID04" href="#">Link4</a>
</body>
</html>

在我的 script.js 中

function checkLocalStorage() {
setInterval(function(){
if("linkValue01" in localStorage){
var storedTime = localStorage.getItem("linkValue01");
var timeNow = new Date().getTime();
var minutesFromLastClick = (timeNow - storedTime)/60000;
if(minutesFromLastClick > 30) {
document.getElementById("linkID01").style.display = "inline";
}
else {
document.getElementById("linkID01").style.display = "none";
}
}
if("linkValue02" in localStorage){
var storedTime = localStorage.getItem("linkValue02");
var timeNow = new Date().getTime();
var minutesFromLastClick = (timeNow - storedTime)/60000;
if(minutesFromLastClick > 180) {
document.getElementById("linkID02").style.display = "inline";
}
else {
document.getElementById("linkID02").style.display = "none";
}
}
if("linkValue03" in localStorage){
var storedTime = localStorage.getItem("linkValue03");
var timeNow = new Date().getTime();
var minutesFromLastClick = (timeNow - storedTime)/60000;
if(minutesFromLastClick > 120) {
document.getElementById("linkID03").style.display = "inline";
}
else {
document.getElementById("linkID03").style.display = "none";
}
}
if("linkValue04" in localStorage){
var storedTime = localStorage.getItem("linkValue04");
var timeNow = new Date().getTime();
var minutesFromLastClick = (timeNow - storedTime)/60000;
if(minutesFromLastClick > 10) {
document.getElementById("linkID04").style.display = "inline";
}
else {
document.getElementById("linkID04").style.display = "none";
}
}
}, 1000);
}


function saveTime01(){
var object = new Date().getTime();
localStorage.setItem("linkValue01", JSON.stringify(object));
document.getElementById("linkID01").style.display = "none";
}
function saveTime02(){
var object = new Date().getTime();
localStorage.setItem("linkValue02", JSON.stringify(object));
document.getElementById("linkID02").style.display = "none";
}
function saveTime03(){
var object = new Date().getTime();
localStorage.setItem("linkValue03", JSON.stringify(object));
document.getElementById("linkID03").style.display = "none";
}
function saveTime04(){
var object = new Date().getTime();
localStorage.setItem("linkValue04", JSON.stringify(object));
document.getElementById("linkID04").style.display = "none";
}

最佳答案

我想你可以这样做 -

  <a class="tracker" id="linkValue1" data-id="1" href="#">Link1</a>
<a class="tracker" id="linkValue2" data-id="2" href="#">Link2</a>
<a class="tracker" id="linkValue3" data-id="3" href="#">Link3</a>
<a class="tracker" id="linkValue4" data-id="4" href="#">Link4</a>

现在在 Javascript 中 -

 $(".tracker").on("click",handleClick);

function handleClick(){
var ele = $(this),
object = new Date().getTime(),
id = ele.data("id");

localStorage.setItem("linkValue"+id, JSON.stringify(object));
document.getElementById("linkID"+id).style.display = "none";
}

function checkLocalStorage() {
var numLinks = $(".tracker").length;
setInterval(function(){
for(var i = 0; i< numLinks.length;++i){
var item = "linkValue"+numLinks[i],
eleID = "linkID"+numLinks[i];
if(item in localStorage){
var storedTime = localStorage.getItem(item);
var timeNow = new Date().getTime();
var minutesFromLastClick = (timeNow - storedTime)/60000;
if(minutesFromLastClick > 30) {
document.getElementById(eleID).style.display = "inline";
}
else {
document.getElementById(eleID).style.display = "none";
}
}
}
}
}

关于javascript - 有没有更好的方法来设置和从 LocalStorage 获取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34283115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com