简体   繁体   English

JavaScript分配内存问题

[英]Javascript Allocation Memory Issue

I've created a page to insert order into a mysql database. 我创建了一个页面以将订单插入mysql数据库。 Under the order the user is able to add up to 9 itens in any quantity specified. 根据该命令,用户可以添加任意数量的最多9个iten。 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) 起初我只使用了一个逗号串接(dados + =''),然后尝试使用array.push来查看它是否可以解决分配内存问题,但是并没有按预期进行(加载时间很慢并且再次陷入内存分配问题)

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循环都是错误的:

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 这是必需的,因为否则counter > 9成为赋值表达式/初始化块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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