JSON将值返回到全局变量

时间:2015-08-01 03:32:32

标签: javascript php ajax json

我的代码看起来像这样:

 var thevariable = 0;

For(){
//somecode using thevariable
$.getJSON('',{},function(e){
//success and i want to set the returned value from php to my variable to use it in the forloop

thevariable = e.result;
});
}

我的主要问题是变量值在整个For循环期间保持" 0",而我只希望它是" 0"在第一个循环中,然后它从PHP返回的结果将其用于for循环。

这是我的真实代码,如果你需要看看:

var orderinvoice = 0;
for(var i=0; i<table.rows.length; i++){
                            var ordername = table.rows[i].cells[5].innerText;
                            var orderqty = ((table.rows[i].cells[1].innerText).replace(/\,/g,'')).replace(/Qty /g,'');
                            var orderprice = (table.rows[i].cells[2].innerText).replace(/\$/g,'');
                            var ordertype = table.rows[i].cells[3].innerText;
                            var orderlink = table.rows[i].cells[4].innerText;

      $.getJSON('orderprocess.php', {'invoice': orderinvoice, 'pay_email': email, 'ord_name': ordername, 'ord_qty': orderqty, 'ord_price': orderprice, 'ord_type': ordertype, 'ord_link': orderlink}, function(e) {
          console.log();
          document.getElementById("result").innerText= document.getElementById("result").innerText + "Order #"+e.result+" Created Successfully ";
         document.getElementById("invoker").innerText = ""+e.invoice;
         orderinvoice = e.invoice;

          if(i+1 == table.rows.length){
            document.getElementById("result").innerText= document.getElementById("result").innerText + "With invoice #" + e.invoice;
          }
 });

2 个答案:

答案 0 :(得分:1)

在一个循环块中,在一个ajax完成之前,另一个将运行并且这是javascript自然处理。对于您的情况,您可以在成功事件结束时调用函数。做这样的事情:

var i = 0;    
doSt();

function doSt() {
    var orderinvoice = 0;

        var ordername = table.rows[i].cells[5].innerText;
        var orderqty = ((table.rows[i].cells[1].innerText).replace(/\,/g, '')).replace(/Qty /g, '');
        var orderprice = (table.rows[i].cells[2].innerText).replace(/\$/g, '');
        var ordertype = table.rows[i].cells[3].innerText;
        var orderlink = table.rows[i].cells[4].innerText;

        $.getJSON('orderprocess.php', { 'invoice': orderinvoice, 'pay_email': email, 'ord_name': ordername, 'ord_qty': orderqty, 'ord_price': orderprice, 'ord_type': ordertype, 'ord_link': orderlink }, function(e) {
            console.log();
            document.getElementById("result").innerText = document.getElementById("result").innerText + "Order #" + e.result + " Created Successfully ";
            document.getElementById("invoker").innerText = "" + e.invoice;
            orderinvoice = e.invoice;

            if (i + 1 == table.rows.length) {
                document.getElementById("result").innerText = document.getElementById("result").innerText + "With invoice #" + e.invoice;
            }
            i++;
            if (i < table.rows.length) doSt();
        });
    }

答案 1 :(得分:1)

我认为您需要一个递归函数,它总是处理行数组中的第一个元素,然后将其拼接并调用自身。例如,像这样:

function getStuff(rows, results) {
    if (rows.length > 0) {
        var ordername = rows[0].cells[5].innerText;

        $.getJSON('orderprocess.php', { 'ord_name': ordername }, function (e) {
            // do some stuff
            results.push('aggregate some things here?');
            rows.splice(0, 1);
            return getStuff(rows, results);
        });
    } else {
        return results;
    }
}

当花费数组时,将返回结果,并在循环结束时返回您想要的任何聚合。然后,您可以随意使用结果。我认为如果更合理的话,你也可以按照你认为合适的方式操作函数内部的DOM。希望这会有所帮助。