简体   繁体   中英

JSON return value to global variable

Simply my code looks like this:

 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;
});
}

my main problem that the variable value stays "0", during the whole For loop, while i only want it to be "0" at the first loop, then it takes the result returned from PHP to use it on for loop.

here it my real code if you need to take a look:

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;
          }
 });

in a loop block, before one ajax complete other one will be run and this's javascript natural treatment. For your case you can call a function at the end of success event. Do something like this:

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();
        });
    }

I think you need a recursive function that always deals with the first element in your rows array and then splices it off and calls itself. For example, something like this:

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;
    }
}

When the array is spent, results will be returned with whatever aggregate you wanted at the end of the cycle. Then, you can do as you please with the results. I think you can also manipulate the DOM inside the function as you see fit if that makes more sense. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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