我有一个要求,其中有一个java脚本代码,它可以进行ajax调用并在页面加载时将用户登录保存在数据库中。但问题是,每次用户刷新页面时,代码都会被执行。我只想在 10 秒后刷新页面而不是每次页面刷新时执行该语句。如果页面在 10 秒之前刷新,我必须停止执行代码流。
最佳答案
此代码执行全部 10 秒(或更长时间),由页面重新加载触发:
您需要存储时间(在 localStorage 中),重新加载时检查现在是否晚于存储时间+ 10 秒:
function store(){
//store time:
now=new Date().getTime();
localStorage.setItem("stored",now);
//your password storing
}
window.onload=function(){
now=new Date.getTime();
if(!localStorage.getItem("stored")){
//no time stored, so lets store
store();
return;
}
last=localStorage.getItem("stored");
//if now is later than last+10seconds
if(now>last+1000*60*10){
store();
}
}
关于javascript - 仅在 10 秒后刷新页面时才执行 JavaScript 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41321887/