简体   繁体   中英

Javascript Allocation Memory Issue

I've created a page to insert order into a mysql database. Under the order the user is able to add up to 9 itens in any quantity specified. Because of this I created a string concatenation loop. At first I used just a commom string concatenation(dados += ''), then I tried to use an array.push to see if it would solve the allocation memory problem but it didn't do as expected(loading time very slow and get caught in memory allocation issue again)

Using the first approach the error message was: "Allocation Size Overload" Now the current error message is: "uncaught exception: out of memory" Please take a look at my code and see what you think:)

function insertOrder(){
var counter = 0;
var lenz  = new Array();
var data;
var id      = 0;

var patientName  = $('#patientName').attr("value");
var phone1       = $('#phone1').attr("value");
var phone2       = $('#phone2').attr("value");
var email        = $('#email').attr("value");
var status       = $('#status').attr("value");
var atendantName = $('#atendantName').attr("value");
var referee      = $('#referee').attr("value");

$("select[name='lenz']").each(function(){
    if (counter == 0){
        lenz.push('lenzId='+$(this).val());
        counter++;
    }else{
        lenz.push('&lenzId'+counter+'='+$(this).val());
    }
    counter++;
}); 

for (counter < 9; counter++;){
    lenz.push('&lenzId'+counter+'=0');
}


counter = 0;

$("input[name='quant']").each(function(){

    if (counter == 0){
        lenz.push('quantity='+$(this).val());
        counter++;
    }else{
        lenz.push('&quantity'+counter+'='+$(this).val());
    }
    counter++;

});

for (counter < 9; counter++;){
    lenz.push('&quantityId'+counter+'=0');
}


var paymentMethod = $('#paymentMethod').attr("value");
var trancheNumber = $('#trancheNumber').attr("value");
var discount      = $('#discount').attr("value");
var totalAmount   = $('#totalAmount').attr("value");
var trancheAmount = $('#trancheAmount').attr("value");
var remarks       = $('#remarks').attr("value");

if(patientName == ''){
    alert('Patient Name is missing');
    return false;
}

if(confirm("Are you sure you want to include this order?")){
    data += lenz.join();
    alert(data);
    data += '&id='+id+'&patientName='+patientName+'&phone1='+phone1+'&phone2='+phone2+'&email='+email+'&status=';
    data += status+'&atendantName='+atendantName+'&referee='+referee+'&paymentMethod='+paymentMethod+'&trancheNumber='+trancheNumber+'&discount='+discount;
    data += '&totalAmount='+totalAmount+'&trancheAmount='+trancheAmount+'&remarks='+remarks;



    alert(data);

    $.ajax({
        type: "POST",
        url: caminho+"/view/includeOrder.php?acao=salvar",
        timeout: 20000,
        data: data,
        success: 
            function(data){
                if(jQuery.trim(data) == 'ok'){
                    alert('Order sucessfully included!');
                    if (id == 0){
                        $("#includeOrderform")[0].reset();
                        $("#select-patient").html('Paciente: <button class="btn btn-theme btn-search margintop10 pull-left" type="button" onCLick="popupCenter(\'selecionaPaciente.php\', \'selecionaPaciente\', 750, 500);" >Pesquisar</button>');
                    }else{
                        mostrarTela('maintainOrder');
                        $('html, body').animate({ scrollTop: 0 }, 'slow');
                    }

                }
                else{
                    alert('Error saving Order!');
                }
            }
    });
}
}

Basically, all your for loops are wrong:

for (counter < 9; counter++;)

it should be

for (; counter < 9 ; counter++)

The first ; is necessary here since otherwise counter > 9 becomes the assignment expression / initialization block

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