如何处理大型jquery每个循环而不会崩溃浏览器(主要是firefox)

时间:2017-03-24 10:14:15

标签: javascript jquery iframe

我根据弹出窗口中iframe的用户选择,制作了一个javascript代码,用于在父iframe中创建一些html元素。

我的代码如下。

每个循环可能包含 50到150次迭代。有时当我调用函数AddProcedures时,Mozilla在迭代期间崩溃,是否有任何方法可以解决此问题或减少浏览器负载。我尝试将setTimeout放在一些代码块之后,但它没有帮助。

我还将for循环更改为每个jquery,只是为了尝试这是否有帮助。

     var tot_codes = 0;   
 function doAddProcedure(thisVal,resolve){       
    var enc_id  = $("#cpt_encounter").val();
    var rowId   = window.parent.$("[data-encounterBilled='"+ enc_id +"']").closest('tr').attr('id'); // table row ID 
    var recalc  = parseInt(rowId.substring(rowId.indexOf('id-')+3));
    var countval = $("#last_id").val(); 
                //Load issue Fix 
                //Old code
                //for (i = 0; i < document.cpt.elements.length; i++) {         
                //if (document.cpt.elements[i].type == "checkbox") {
                //if (document.cpt.elements[i].checked == true) {
            $('#cpt input:checkbox:checked').each(function(){
                setTimeout(function() { }, 100);  
                var currentCodeMod   = $(this).attr("data-codemod");
                var currentCodeTypid = $(this).attr("data-codeTypeid");
                if (currentCodeMod == null)
                    return;                
                tot_codes++;
                var nextcntval  = parseInt(countval) + 1;                
                var code_no     = $(this).attr("id").replace("cpt_chkbx", "");
                var Code        = $("#codeVal" + code_no).val();
                var just        = $("#codeType" + code_no).val();                
                var categoryId   = $("#codeType" + code_no).data("categoryid"); //Added to get category //Bug Fix  
                var Name        = $("#cpttext" + code_no).val();
                var codedisp    = Code + ':' + Name;
                var Modifier    = '';

                //if($("#modifier_setngs").val() == 1){
                    var Modifier1   = $("#modcpt1"+code_no).val() != '' ? $("#modcpt1"+code_no).val() + ":" : '';
                    var Modifier2   = $("#modcpt2"+code_no).val() != '' ? $("#modcpt2"+code_no).val() + ":" : '';
                    var Modifier3   = $("#modcpt3"+code_no).val() != '' ? $("#modcpt3"+code_no).val() + ":" : '';
                    var Modifier4   = $("#modcpt4"+code_no).val() != '' ? $("#modcpt4"+code_no).val() + ":" : '';                
                    Modifier        = Modifier1 + Modifier2 + Modifier3 + Modifier4;  
                //}
                var codetableid = $("#code_tab_id" + code_no).val();                               
                var j           = countval; /* lineitem wise uniqid */
                //var encounter   = window.parent.$('#codetype-' + j).closest('tr.charge-entry-row').find('.expand-actions').attr('data-encounterid');
                var encounter        = enc_id;

                var codeforarr  = encounter + ':' + just + ':' + Code + '::' + Modifier;    


                window.parent.pushToArray(codeforarr);
                var f           = 0;
                $(window.parent.$("#codetype-" + countval).parents().find('.inner-table .charge-entry-row')).each(function() {
                      //console.log($(this).find('.inputStringHidden').val() + '/' + codeforarr);
                      if ($(this).find('.inputStringHidden').val() == codeforarr) {                          
                        var exis_row_id = $(this).find('.inputStringHidden').attr('id').split('inputStringHidden-');
                        j = exis_row_id[1].toString();                       
                        f = 1;        
                        $(this).find('.front_pay_codes_display').trigger('click');
                    }
                });   

                if (f == 0) {

                    if (window.parent.document.getElementById('inputStringHidden-' + countval).value != '') {
                        window.parent.$("#codetype-" + countval).children().parent().parent().parent().siblings().find('.addnew-row').attr('data-rowadd', 'fromdb').trigger("click");
                        countval = parseInt(countval) + 1;
                        j        = countval.toString();
                    }
                    window.parent.$("#add-" + countval).trigger("click");
                    window.parent.$("#codetype-" + (countval)).val(currentCodeTypid);
                    window.parent.$("#codetype-" + countval).trigger("change");
                }               
                window.parent.document.getElementById('inputString' + j).value = codedisp;
                window.parent.document.getElementById('category-' + j).value = categoryId; //Added to set category //Bug Fix
                window.parent.document.getElementById('string_id' + j).value = Code;
                window.parent.document.getElementById('string_value' + j).value = Name;
                window.parent.document.getElementById('inputStringHidden-' + j).value = codeforarr;
                window.parent.document.getElementById('codetableid-' + j).value = codetableid;
                window.parent.$("#inputString" + j).closest('tr.charge-entry-row').find('.val-change-bit').val(1);
                setTimeout(function() {}, 100);
               window.parent.$("#suggestions" + j).hide();                   
                window.parent.$("#inputString" + j).focus();                                  
                var uniqidHidden = window.parent.$("#inputString" + j).closest('tr.charge-entry-row').find('.uniqid-hidden').val();
                window.parent.$('#add-modifier' + j).parent().find('.displaymode-elem').text(Modifier);
                //if($("#modifier_setngs").val() == 1){
                    if (f == 0) {  
                        window.parent.$('#add-modifier' + j).parent().find('.displaymode-elem').addClass('area-hide');
                    }else{
                        window.parent.$('#add-modifier' + j).parent().find('.displaymode-elem').removeClass('area-hide');
                    }
                    window.parent.$('#modifiers' + uniqidHidden).val(Modifier);
                    arr = Modifier.split(':');
                    window.parent.$('#modifier-' + uniqidHidden + '-1').val(arr[0]);
                    window.parent.$('#modifier-' + uniqidHidden + '-2').val(arr[1]);
                    window.parent.$('#modifier-' + uniqidHidden + '-3').val(arr[2]);
                    window.parent.$('#modifier-' + uniqidHidden + '-4').val(arr[3]);
                //}
                var eclaimPlanActive = window.parent.$('#eclaimPlanActive').val();
                var price = $("#cpt_price" + code_no).val();

                var price_level = $("#pricelevelhidden" + code_no).val();   

                if (price == 0 || price == null)
                    price = 0;
                window.parent.$('#price-' + j).val(price);
                window.parent.$('#bill-pricelevel-' + j).val(price_level);

                /** Charge Total **/
                var chargeTotal = window.parent.$('#charge-total').text().replace(/[\s\,]/g, '').trim();
                if (chargeTotal == '')
                    chargeTotal = parseFloat(0);
                var tempChargeTotal = parseFloat(chargeTotal) + parseFloat(price);
                window.parent.$('#charge-total span').html(tempChargeTotal);
                /** End **/
                var units = $("#cpt_units" + code_no).val(); //data[0]['units']
                if (units == 0 || units == null)
                    units = 1;
                window.parent.$('#qty-' + j).val(units);
                var tax = 0;
                var disc = 0;

                setTimeout(function() { }, 100);
                if (window.parent.$('#tax-' + j).length > 0)
                    tax = window.parent.$('#tax-' + j).val().replace(/[\s\,]/g, '');

                if (window.parent.$('#discount-' + j).length > 0)
                    disc = window.parent.$('#discount-' + j).val().replace(/[\s\,]/g, '');

                var amount = price * units;
                amount = amount + ((amount * tax) / 100);
                if (disc > 0) {
                    var p = price * units;
                    var dc = ((p * disc) / 100);
                    amount = amount - dc;
                }
                /**  Discount Total **/
                var disTotal = window.parent.$('#discount-total').text().replace(/[\s\,]/g, '').trim();
                if (disTotal == '')
                    disTotal = parseFloat(0);
                var tempDisTotal = parseFloat(disTotal) + parseFloat(dc);
                if (isNaN(tempDisTotal) == true) {
                    tempDisTotal = '0.00';
                }
                window.parent.$('#discount-total').html(tempDisTotal);
                /**  End **/

                if (!parseFloat(window.parent.$('#lineitempaid-' + j).val()))
                    window.parent.$('#lineitempaid-' + j).val('0.00');
                window.parent.$('#total-' + j).val(amount);
                window.parent.$('#remainder-' + j).val(amount);
                window.parent.$('#hidremainder-' + j).val(amount);

                //insurance and patient_share
                if (eclaimPlanActive == '1') {
                    var codetableid = window.parent.$('#codetableid-' + j).val();
                    var parentRowVal = parseInt(window.parent.$('#codetableid-' + j).closest('tr').find('.row_value').attr('data-rowvalue')) - 1;
                    var insData1 = window.parent.$('tr#row-id-' + parentRowVal + ' .encounter').attr('data-insdata1');
                    window.parent.getBenefitCategoryOfCode(j, codetableid, insData1);
                    if (window.parent.$('#have-benefit').val() == '0') {
                        var insData2 = window.parent.$('tr#row-id-' + parentRowVal + ' .encounter').attr('data-insdata2');
                        window.parent.getBenefitCategoryOfCode(j, codetableid, insData2);
                    }
                }
                var passParams = '';                    
                window.parent.calculate('qty-' + j, passParams);


                //Updating the title
                window.parent.$("#inputString" + j).closest('tr.charge-entry-row').find('.fi-searchitems').attr('data-original-title', codedisp);
                if (f == 0) { 
                //Display DIv  :: Relace with new values 
                var cloneLabel = window.parent.$("#inputString" + j).closest('tr.charge-entry-row').find('.show_label').clone();
                //In Edit Mode 
                if (cloneLabel.find('.displaymode-elem').length > 0) {
                    var $max_length = 16;
                    var trucncateCode = Name;
                    if (trucncateCode.length > $max_length)
                        trucncateCode = trucncateCode.substring(0, $max_length) + '...';


                    cloneLabel.find('.front_pay_codetype').text(just + ":");
                    cloneLabel.find('.front_pay_code').text(trucncateCode);
                    cloneLabel.find('.displaymode-elem').removeClass('area-hide');
                    cloneLabel.removeClass('area-hide');
                    cloneLabel.find("input[name='code_type']").val(just).attr('id', 'code_type-' + j);
                    cloneLabel.find("input[name='string_id']").val(Code).attr('id', 'string_id-' + j);
                    cloneLabel.find("input[name='string_value']").val(Name);
                    var dataType = $("#inputString" + j).closest('tr.charge-entry-row').find('.data_type').val();
                    if (dataType == "lab") {
                        var lab = $("select[name='lab_type']").find('option:selected').val();
                        if (cloneLabel.find("input[name='lab_type']").length > 0) {
                            cloneLabel.find("input[name='lab_type']").val(lab);
                        } else {
                            cloneLabel.append('<input type="hidden" name="lab_type" class="lab_type" value="' + lab + '">');
                        }
                    } else {
                        cloneLabel.find("input[name='lab_type']").remove();
                    }
                    setTimeout(function() { }, 100);  
                    //Making the view
                    cloneLabel.removeClass('area-hide');
                    window.parent.$("#inputString" + j).closest('tr.charge-entry-row').find('.front_pay_codes_editelem').removeClass('area-show').addClass('area-hide');
                    window.parent.$("#inputString" + j).closest('tr.charge-entry-row').find('.show_label').replaceWith(cloneLabel);                        
                    window.parent.$("#inputString" + j).closest('tr.charge-entry-row').find('.show_autosuggest').html('');

                }
                window.parent.$("#codetype-" + countval).children().parent().parent().parent().siblings().find('.addnew-row').attr('data-rowadd', 'fromdb').trigger("click");
                countval = nextcntval.toString();
               }
                else {                                         
                    window.parent.$("#suggestions" + j).hide();                    
                    countval = countval.toString();
                }
                setTimeout(function() { }, 150);

    });
 window.parent.reCalculatePlanEncounterWise(recalc);
resolve("Success!");
} 
function doAddPromise(thisVal){
return new Promise(function(resolve){
doAddProcedure(thisVal,resolve);
});
} 

function AddProcedures(thisval)
{
    $('#procedureSaveButton').text('Processing....');
   // $('.fa-spinner').removeClass('hide');
    top.ShowAjaxLoader('Loading..');
    setTimeout(function(){        
     }, 2000); 
    //top.ShowAjaxLoader('Loading..');
    setTimeout(function(){ 
   $.when(doAddPromise(thisval)).done(function(){     
      if (tot_codes > 0) { 
         window.parent.phFrntpayClosePopup();
         top.notification('Successfully added the procedures.');  
         //top.HideAjaxLoader('Loading..');
         top.HideAjaxLoader();
         $('body').removeClass('modal-open');

    } else {          
        top.notification('Please select atleast one procedure.');
        $('#procedureSaveButton').text('Add Procedures');
        //$('.fa-spinner').addClass('hide');
        top.HideAjaxLoader();

    }           
    }); 
   }, 10); 
}

0 个答案:

没有答案