Javascript分配内存问题

时间:2016-05-25 19:15:41

标签: javascript php jquery memory-management

我创建了一个将命令插入mysql数据库的页面。 根据订单,用户可以以指定的任何数量添加最多9个itens。因此我创建了一个字符串连接循环。起初我只使用了一个通用字符串连接(dados + =''),然后我尝试使用array.push来查看它是否会解决分配内存问题,但它没有按预期执行(加载时间非常慢和再次陷入内存分配问题)

使用第一种方法错误消息是:“分配大小过载” 现在当前的错误消息是:“未捕获的异常:内存不足” 请看一下我的代码,看看你的想法:)

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

1 个答案:

答案 0 :(得分:1)

基本上,你所有的for循环都是错误的:

for (counter < 9; counter++;)

应该是

for (; counter < 9 ; counter++)

此处需要第一个;,否则counter > 9成为赋值表达式/初始化块