简体   繁体   English

在局部范围内定义全局范围的变量

[英]define variable of global scope in local scope

I am working in javascript. 我正在使用JavaScript。 I am trying to reload the page when user is logged in. I am polling to constantly check user data. 我正在尝试在用户登录时重新加载页面。我正在轮询以不断检查用户数据。 Find the codes the below 在下面找到代码

var check;
 function poll(){
        return $.ajax({
            url: 'http://localhost:3000/poll',
            type:'GET',
            xhrFields: { withCredentials: true }
        }).done(function(data){
              console.log(data); //  if user is logged in ,server sends the user object else it doesn't
              if(typeof data.name !== "undefined"){
                  check = '1';   // Browser doesnt remember check was 1 after reload
                  window.location.reload();
                    } else
                    {
                        setTimeout(function(){ poll(); }, 5000);
                    }


                });
    }

    if(typeof check == 'undefined'){     // Problem is here on reload, browser doesnt remember that i set `check = 1` in above condition. 
    setTimeout(function(){ poll(); }, 5000);
    }

I am trying to make workflow like , 我正在尝试使工作流程像

1) If user is not logged in , polling should continue. 1)如果用户未登录,则应继续轮询。 It should keep checking the wheather user is logged in or not . 它应继续检查是否有小麦用户登录。

2) If user is logged in , Reload page automatically and polling stops 2)如果用户已登录,则自动重新加载页面,并且轮询停止

您可以使用浏览器或cookie的本地存储(HTML5)。

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

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