全局变量未在jquery中设置

时间:2015-02-15 21:55:10

标签: jquery function variables definition mapael

$(function(){
    var bwr_w = null; //global variable

//below function gets the dynamic data 
    function myfunc() { 
        var val = '2011'
        $.ajax({
            type: "POST",
            url: "allmaps.php",
            data: "year="+val ,
            cache: false,
            success: function(result){                  
                bwr_w= result.replace(/\s+/g, ''); //want to set the data again

            }
        });
    }

    myfunc(); //my dynamic function gets called

    $(".container_map").mapael({                                    
        map : {
            name : "usa_states"
        },
        plots: {
                bwr_w //this should work as per myfunc()
        }
    });

});

我总是把bwr_w的值变为null,即使我在ajax返回中得到一些值 我想将我的bwr_w设置为全局变量,这样当我从ajax得到一些结果时,它应该改变我的地图引脚。

2 个答案:

答案 0 :(得分:0)

问题是因为$.ajax调用是异步的。这意味着在从AJAX调用返回数据之前,您的myfunc已退出。要解决此问题,请将所有代码依赖于回调中返回的数据:

$(function () {
    var bwr_w = null; //global variable

    //below function gets the dynamic data 
    function myfunc() {
        var val = '2011'
        $.ajax({
            type: "POST",
            url: "allmaps.php",
            data: "year=" + val,
            cache: false,
            success: function (result) {
                bwr_w = result.replace(/\s+/g, ''); //want to set the data again
                $(".container_map").mapael({
                    map: { name: "usa_states" },
                    plots: { bwr }
                });               
            }
        });
    }

    myfunc();
});

如果您希望在每次调用myfunc时执行不同的逻辑,请将其作为回调函数传递:

$(function () {
    //below function gets the dynamic data 
    function myfunc(callback) {
        var val = '2011'
        $.ajax({
            type: "POST",
            url: "allmaps.php",
            data: "year=" + val,
            cache: false,
            success: function (result) {
                var bwr_w = result.replace(/\s+/g, ''); //want to set the data again
                callback(bwr_w);
            }
        });
    }

    myfunc(function (bwr) {
        $(".container_map").mapael({
            map: { name: "usa_states" },
            plots: { bwr }
        });
    });
});

答案 1 :(得分:0)

见这里

$(function(){
    var bwr_w = null; //global variable

    //below function gets the dynamic data 
    function myfunc(then) { 
        var val = '2011'
        $.ajax({
            type: "POST",
            url: "allmaps.php",
            data: "year="+val ,
            cache: false,
            success: function(result){                  
                then && then(result);                   
            }
        });
    }

    myfunc(function() {

      bwr_w= result.replace(/\s+/g, ''); //want to set the data again

    $(".container_map").mapael({                                    
        map : {
            name : "usa_states"
        },
        plots: {
                bwr_w //this should work as per myfunc()
        }
      });
    }); 
});