提醒框出现后,我如何在10秒后刷新?

时间:2018-11-09 09:49:45

标签: javascript reactjs

所以我一直在尝试自动注销,但是我无法找到一种在警报框发出10秒后注销的方法(警报框询问您是否要注销是否还没有离开该站点,但正在浏览某些信息并且不想登录out)自动显示。基本上,我想让它自动退出,如果您在选择的时间(例如10秒)后没有按任何警报框上的任何内容。 这是代码!

var refresh_rate = 5; // <-- In seconds, change to your needs
var reset_rate = 7; // <-- In seconds logs out after not pressing OK or cancel in automatic logout
var last_user_action = 0;
var has_focus = false;
var lost_focus_count = 0;
var focus_margin = 10; // If we lose focus more then the margin we want to refresh


function reset() {
    last_user_action = 0;
    console.log("Reset");
}

function windowHasFocus() {
    has_focus = true;
}

function windowLostFocus() {
    has_focus = false;
    lost_focus_count++;
    console.log(lost_focus_count + " <~ Lost Focus");
}

setInterval(function () {
    last_user_action++;
    refreshCheck();
}, 1000);

function refreshCheck() {
    var focus = window.onfocus;
    if ((last_user_action >= refresh_rate && !has_focus && document.readyState == "complete") || lost_focus_count > focus_margin) {
      var answer = confirm("Session About To Timeout\n\n       You will be automatically logged out.\n       Confirm to remain logged in.")
        if (answer){

            refresh_rate = 10;
        }
        else{
          window.location.reload(); // If this is called no reset is needed
          reset(); // We want to reset just to make sure the location reload is not called.
        }
    }
}

我希望有人可以帮助我解决这个问题!

1 个答案:

答案 0 :(得分:1)

如果使用confirm(或alertprompt,则不能使用1990年代的任何文物)。他们使主要的JavaScript线程陷入停顿状态(大多数情况下,现在已经有细微差别了),这意味着您(大多数情况下)不能做其他任何事情。

为此,您需要将confirm替换为显示消息的DOM元素,这不会阻止JavaScript主线程。