为什么js全局变量没有被内部ajax函数改变? [英] Why the js global variable not changed by internal ajax function ?

查看:144
本文介绍了为什么js全局变量没有被内部ajax函数改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var rcode = 0;

//微博认证函数
        function CNY_CheckWeiDo() {            
            

            $.post("do_chy_anthorize.aspx", function (data) {
                var result = eval('(' + data + ')');
                if (result.code == undefined) {                    
                    isLogin = false;
                    rcode = 2;
                    //alert("rcode is 2 :"+rcode);
                }
                if (result.code == 1) {                    
                    isLogin = true;
                    rcode = 1;
                    //alert("rcode is 1 :" + rcode);
                }
                if (result.code == 0) {
                    isLogin = false;
                    rcode = 0;
                    //alert("rcode is 0 :" + rcode);
                    document.location.href = "http://localhost:8870/docheck.aspx" ;
                    
                }
                if (result.code == -2) {                   
                    isLogin = false;
                    rcode = -2;
                    //alert("rcode is -2 :" + rcode);
                    document.location.href =  "http://localhost:8870/docheck.aspx";
                    
                }

                if (result.code == -1) {                    
                    isLogin = false;
                    rcode = -1;
                    //alert("rcode is -1 :" + rcode);
                    document.location.href = "http://localhost:8870/docheck.aspx";                   
                }


            });

            alert("return code :" + rcode);            
            //rcode = 1;

            return rcode;
        }





作为代码,当我提醒'rcode'的值时,为什么'rcode'总是返回0 ?



但是在功能中,当我发出警报时,它的价值已经改变了。不能在ajax函数中改变全局变量吗?

谁可以说实话?谢谢



as the code ,when i alert the value of 'rcode', why the 'rcode' always returns 0 ?

but in the function , when i alert , it's value has changed . can't the global variable be changed in the ajax function ?
who can tell the truth ? thanks

推荐答案

.post( do_chy_anthorize.aspx function (data){
var result = eval ' (' + data + ' )');
if (结果。 code == undefined ){
isLogin = false ;
rcode = 2 ;
// alert(rcode is 2 :+ rcode);
}
if (result.code == 1 ){
isLogin = true ;
rcode = 1 ;
// alert(rcode is 1:+ rcode);
}
if (result.code == 0 ){
isLogin = < span class =code-keyword> false ;
rcode = 0 ;
// alert(rcode is 0:+ rcode);
document location .href = < span class =code-string> http:// localhost:8870 / docheck.aspx;

}
if (result.code == -2){
isLogin = ;
rcode = -2;
// alert(rcode is -2:+ rcode);
document location .href = http:// localhost:8870 / docheck.aspx;

}

if (result.code == -1){
isLogin = ;
rcode = -1;
// alert(rcode is -1:+ rcode);
document location .href = http:// localhost:8870 / docheck.aspx;
}


});

alert( return code: + rcode);
// rcode = 1;

返回 rcode;
}
.post("do_chy_anthorize.aspx", function (data) { var result = eval('(' + data + ')'); if (result.code == undefined) { isLogin = false; rcode = 2; //alert("rcode is 2 :"+rcode); } if (result.code == 1) { isLogin = true; rcode = 1; //alert("rcode is 1 :" + rcode); } if (result.code == 0) { isLogin = false; rcode = 0; //alert("rcode is 0 :" + rcode); document.location.href = "http://localhost:8870/docheck.aspx" ; } if (result.code == -2) { isLogin = false; rcode = -2; //alert("rcode is -2 :" + rcode); document.location.href = "http://localhost:8870/docheck.aspx"; } if (result.code == -1) { isLogin = false; rcode = -1; //alert("rcode is -1 :" + rcode); document.location.href = "http://localhost:8870/docheck.aspx"; } }); alert("return code :" + rcode); //rcode = 1; return rcode; }





作为代码,当我提醒'rcode'的值时,为什么'rcode'总是返回0 ?



但是在功能中,当我发出警报时,它的价值已经改变了。不能在ajax函数中改变全局变量吗?

谁可以说实话?谢谢



as the code ,when i alert the value of 'rcode', why the 'rcode' always returns 0 ?

but in the function , when i alert , it's value has changed . can't the global variable be changed in the ajax function ?
who can tell the truth ? thanks


原因是ajax post需要时间,之前你从CNY_CheckWeiDo方法返回并且初始值为rcode
the reason is when ajax post take time and before that you are returning from the CNY_CheckWeiDo method and having initial value as rcode


replace alert( 返回代码:+ rcode);



with

setTimeout(function(){alert(rcode)},5000);





这里5000毫秒(5秒)延迟运行警报。



实际上我在这里延迟5秒。因此,在AJAX帖子之后执行警报可以完成其工作。只是减少或增加时间延迟,看看发生了什么?
replace alert("return code :" + rcode);

with
setTimeout (function (){ alert (rcode)}, 5000);


here 5000 milliseconds( 5 Seconds) is delay to run alert.

Actually here i am giving 5 seconds delay. So that alert execute after AJAX post can finish its work. Just decrease or increase time delay and see whats happens?


这篇关于为什么js全局变量没有被内部ajax函数改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆