我想在单击按钮后将其禁用 10 分钟,即使在刷新时它仍将保持禁用状态。我怎样才能用 javascript 或 jquery 制作它?我似乎无法为我的代码做到这一点。
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js">
$(document).ready(function(){
var limit = 10*60*1000;
btn.attr("disabled", true);
btn.css('cursor','not-allowed');
message.text('wrong answer, please wait 10 minutes before trying again');
var timer = setTimeout(disableMyButton, limit);
function disableMyButton(){
btn.removeAttr("disabled");
message.text('');
localStorage.setItem("isMyButtonDisabled", "true");
}
if(localStorage.getItem("isMyButtonDisabled") == "true"){
message.text('');
btn.removeAttr("disabled");
window.clearTimeout(timer);
}
});
</script>
最佳答案
你的想法是正确的。使用 localStorage 但将时间戳放在那里。然后当页面加载时,从 localStorage 中获取时间戳。如果 localStorage 时间戳与现在之间的差异超过 10 分钟,则启用该按钮。如果不是,请适当设置超时。唯一的问题是保持 localStorage 干净。确保在 10 分钟结束时清除时间戳。
关于javascript - 如何使按钮在刷新后禁用 10 分钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51898442/