简体   繁体   English

带有if语句的Window.onblur

[英]Window.onblur with if statement

I am building kind of clock and I strongly need to use settimeout() . 我正在建立一种时钟,我强烈需要使用settimeout()

However, Google Chrome and Safari doesn't allow settimeout() when tab inactive. 但是,当选项卡处于非活动状态时,Google Chrome和Safari不允许settimeout() So, I decided to reloading page when tab active again. 因此,我决定在选项卡再次处于活动状态时重新加载页面。 (I know, it's not proper way to do it, but I am new at javascript and tried to learn.) I saw this answer and tried to implement to my case. (我知道,这不是正确的方法,但是我是javascript语言的新手,并且尝试学习。)我看到了这个答案,并尝试针对我的情况实施。 But there is a challenge, I have 3 modals in this page and I don't want reload page when modals are open. 但是有一个挑战,我在此页面中有3个模态,并且当模态打开时,我不希望重新加载页面。

So, I added a global variable equels an integer then changed the value of variable when modals are open. 因此,我添加了一个全局变量,使它等于一个整数,然后在模态打开时更改了变量的值。

Here is my code, 这是我的代码,

var modalCheck = 2;

function openModal() {
    var doneModal = document.getElementById("done");
    var done = document.getElementsByClassName("closeMessage")[0];

    modalCheck = 1;

    doneModal.style.display = "block";

    done.onclick = function() {
        doneModal.style.display = "none";
        modalCheck = 2;
    }

    window.onclick = function(event) {
        if (event.target == doneModal) {
            doneModal.style.display = "none";
            modalCheck = 2;
        }
    }
}

window.onblur = function() {
    window.onfocus= function () {
        if (modalCheck = 2) {
            console.log(modalCheck);
            location.reload(true);
        }
    }
}; 

Page reloading is still working, but if modals are opened page reaload immediately. 页面重新加载仍在工作,但是如果打开模式,则页面将立即重新加载。

My question is why this is not working? 我的问题是为什么这不起作用? Is there a syntax error or something like that? 是否存在语法错误或类似的东西? What is the solution? 解决办法是什么?

Here's the problem: 这是问题所在:

if ( modalCheck = 2 ) {

This "if" condition is always true, because you're actually assigning the value "2" to "modalCheck" rather than checking whether "modalCheck" equals "2". 此“ if”条件始终为true,因为实际上是将值“ 2”分配给“ modalCheck”,而不是检查“ modalCheck”是否等于“ 2”。 The assignment resolves to the value "2", which is treated as a "truthy" expression in your if statement. 赋值解析为值“ 2”,在if语句中将其视为“真实”表达式。 So this if statement never fails. 因此,此if语句永远不会失败。 I recommend 我建议

if ( modalCheck === 2 ) {

Others have suggested "if ( modalCheck == 2 ) {", but it's generally better to compare using === rather than == to avoid unexpected weirdness. 其他人建议使用“ if(modalCheck == 2){”,但是通常最好使用===进行比较,而不是使用==进行比较,以避免意外的怪异。

i don't know what your html part is, try this code 我不知道您的html部分是什么,请尝试以下代码

   var modalCheck = 2;

    function openModal() {
        var doneModal = document.getElementById("done");
        var done = document.getElementsByClassName("closeMessage")[0];

        modalCheck = 1;

        doneModal.style.display = "block";

        done.onclick = function() {
            doneModal.style.display = "none";
            modalCheck = 2;
        }

        window.onclick = function(event) {
            if (event.target == doneModal) {
                doneModal.style.display = "none";
                modalCheck = 2;
            }
        }
    }

    window.onblur = function() {
     this.addEventListener('focus',function(){
        if (modalCheck == 2) {
                console.log(modalCheck);
                location.reload(true);
         }});

        }; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM