Front-end page development, often need to asynchronously check the login state, each time to copy the previous method of writing, compared to the cumbersome maintenance, solid check the login state to a JS.
(function(){ //Login Status 1 Login valid 2 Invalid login State 3 Request timed out varstatus; //Status expiry time stamp varStatus_exprie_timestamp; //Default Time-out (in seconds) varDefault_exprie_time = 60; //the qualified protocol name can only be HTTPS or HTTP varLP = "https:" = = Document.location.protocol? ' https: ': ' http: '; //detecting whether to log in //login_user_id User ID | session_id Session ID | Callback callback function after successful failure timeout //Timeout time 5 seconds functionCheck_login (login_user_id,session_id,callback) {//A status that is not expired and the status is not 3 (Request timed out) directly called callback does not update the expiration timestamp if(Status &&!status_is_expire () && Status! = 3) {callback (status); return ; } //format Judgment if(!check_login_user_id (login_user_id) | |!check_session_id (session_id)) {EXEC (2, callback); return ; } //build request-related variables varAjax_url = lp+ '//host/xxx '; vardata = { ' login_user_id ': login_user_id,' session_id ': session_id,' Onlyjson ': 0, ' t ':NewDate (). GetTime ()}; //Request$.ajax ({type:"Get", Url:ajax_url, Data:data, timeout:5000, DataType:"Jsonp", Success:function(data) {if(data.rtn==0) {EXEC (1, callback); }Else{EXEC (2, callback); }}, Error:function(data, Error_type, XHR) {if(error_type== ' timeout ') {EXEC (3, callback); }Else{EXEC (2, callback); } } }); } //with Check_login but no need to preach login_user_id and session_id functionEsay_check_login (callback) {varlogin_user_id =get_login_user_id (); varsession_id =get_session_id (); Check_login (Login_user_id,session_id,callback); } //determine the user ID format functioncheck_login_user_id (login_user_id) {return/^[0-9]{4,}$/. Test (LOGIN_USER_ID); } //determine the session ID format functioncheck_session_id (session_id) {return/^[0-9a-fa-f]{20,40}$/. Test (session_id); } //Get user ID functionget_login_user_id () {return_get (' login_user_id ')? _get (' login_user_id '): GetCookie (' login_user_id ')); } //Get session ID functionget_session_id () {return_get (' session_id ')? _get (' session_id '): GetCookie (' session_id ')); } //Gets the current timestamp (in seconds) //add_sec selectable as the current timestamp (seconds) plus the corresponding number of seconds functionGet_now_timestamp (add_sec) {vartimestamp = Date.parse (NewDate ())/1000;if(Add_sec &&!)IsNaN (add_sec)) {Timestamp+=parseint (ADD_SEC); } returntimestamp; } //determine whether the state timed out functionStatus_is_expire () {varNow_timestamp =Get_now_timestamp (); returnStatus_exprie_timestamp && Now_timestamp>=status_exprie_timestamp?true:false; } //Get query parameter method function_get (name) {varReg =NewRegExp ("(^|&)" + name + "= ([^&]*) (&|$)"); varr = Window.location.search.substr (1). Match (REG); if(r!=NULL)returnUnescape (r[2]);return NULL; } //setting the state, time-out, and invoking the callback function functionEXEC (_status,callback) {status=_status; Status_exprie_timestamp=Get_now_timestamp (default_exprie_time); Callback (status); } //gets the cookie for the specified name functionGetCookie (name) {varArr=document.cookie.split ('; ‘); if(!arr)return false; for(vari=0;i<arr.length;i++){ varOne=arr[i].split (' = ')); if(One[0]==name)returnOne[1]; } return false; } //throw variables, methodsWindow.check_login =Check_login; Window.esay_check_login=Esay_check_login; Window. LOGIN_SUCC= 1; Window. Login_fail= 2; Window. Login_timeout= 3;}) ();
Calling methods
Check_login (login_user_id,session_id,function(status) {if(status = = Login _SUCC) {//xxx}elseif(status = = login_fail) {// XXX}elseif(status = = login_timeout) {//xxx}});
JS Check Login State method Encapsulation (closure, state cache)