简体   繁体   English

ajax调用后全局变量未更改

[英]global variable does not changed after ajax call

I am using below code 我正在使用下面的代码

var lockonscreens = 1;

jQuery(document).ready(function(e) {
    var noOfSelection = 0; 

        if(lockonscreens == 0){
            // some stuff
        }
        if(lockonscreens == 1){
            // some stuff
        } 

});

function ajaxcall(){
    jQuery.ajax({
        url:
        type:
        data:
        async: false,
        success: function(data){
            lockonscreens = data;
        }
    });
}

jQuery("#").click(function(){
ajaxcall();
});

I am using above code to get some data through ajax and set it to variable and depending on that variable a click event code may happen. 我正在使用上面的代码通过ajax获取一些数据并将其设置为变量,并且根据该变量,可能会发生点击事件代码。

But on ajax call the global variable value doesn't get changed. 但是在ajax调用中,全局变量值不会更改。 It remains the same even if the data changes in ajax. 即使数据在ajax中更改,它也保持不变。

Can anyone let me know what is the issue and how to correct it? 谁能让我知道问题出在哪里以及如何解决?

Remember that ajax is asynchronous , so if you call ajaxCall() and next an other function, ajax start the call to server and the function end. 请记住,ajax是异步的 ,因此,如果您调用ajaxCall()和下一个其他函数,则ajax将开始对服务器的调用,然后函数结束。

When the server respond, the code after success: is executed. 服务器响应时,将执行success:后的代码success:

Make sure you call the function in the success: function 确保success:调用函数success: function

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

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