简体   繁体   中英

define variable of global scope in local scope

I am working in 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. It should keep checking the wheather user is logged in or not .

2) If user is logged in , Reload page automatically and polling stops

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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