读书人

js处理大量数据时会报错,该怎么解决

发布时间: 2012-05-22 18:18:54 作者: rapoo

js处理大量数据时会报错
下面是写在iframe中的方法,本意是将iframe表格中,凡是前面的checkbox被选中的数据,拼成一个表格传输到父窗口中

JScript code
function AddAttendee(){    var trs = $("tr[name='trHcpList']");    var trs_len = trs.length;    var hcpListBody = "";    if(trs_len > 0){        $.each($(trs).find("input[type='checkbox']"), function(){            if(this.checked==true)            {                var tr = $(this).parent().parent();                var contactId = $(tr).find("td[name='tdContactId']").text();                var hcpName = $(tr).find("td[name='tdHcpName']").text();                var specialty = $(tr).find("td[name='tdSpecialty']").text();                var accountName = $(tr).find("td[name='tdAccountName']").text();                var department = $(tr).find("td[name='tdDepartment']").text();                var state = $(tr).find("td[name='tdState']").text();                if (specialty == '')                {                    var specialty1 ='@^^@';                }else{                    var specialty1 =specialty;                }                hcpListBody = "<tr><td class='hcp_list_checkbox_field' width='100px' height='25px'>" +                 "<input type='hidden' name='contactdIdList' value='" + contactId +"'/>" +                 "<input type='hidden' name='hcpNameList' value='" + hcpName +"'/>" +                 "<input type='hidden' name='specialtyList' value='" + specialty +"'/>" +                 "<input type='hidden' name='accountName' value='-" + accountName +"'/>" +                 "<input type='hidden' name='department' value='-" + department +"'/>" +                 "<input type='hidden' name='state' value='-" + state +"'/>" +                 "<input type='hidden' name='specialtyNmList' value='" + specialty1 +"'/>" +                 "<input type='button' value='Delete' onclick='deleteSlectPrd(this);'/></td>" +                 "<td class='hcp_list' width='130px'>" + contactId + "</td>" +                "<td class='hcp_list' width='200px'>" + hcpName + "</td>" +                "<td class='hcp_list' width='150px'>" + specialty + "</td>" +                "<td class='hcp_list' width='200px'>" + accountName + "</td>" +                "<td class='hcp_list' width='150px'>" + department + "</td>" +                "<td class='hcp_list_checkbox_field' width='120px'>" + state + "</td>" +                "<td class='hcp_list' style='display:none;'></td></tr>";                var check = true;                var trs = $(window.parent.document).find("#divHcpList table tr");                $.each(trs, function(i, item){                    if ($(item).find("td input[value='" + contactId + "']").length > 0)                     {                        check = false;                        return;                    }                });                if (check) {                    var temp = $(window.parent.document).find("#divHcpList table").val();                    temp = temp + hcpListBody ;                    $(window.parent.document).find("#hiddivcontianer").val(temp);                    $(window.parent.document).find("#divHcpList table tbody").prepend(temp);                    $(window.parent.document).find("#divHcpListContainer").css("display","block");                    var height = $(window.parent.document).find("#divHcpList table").height()+10;                    $(window.parent.document).find("#divHcpList").css("height", height);                }                 $(this).parent().parent().parent().hide();            }        });        if($(trs).find("input[type='checkbox'][checked]").length == 0){            alert("Please select HCP.");            return;        }    }    var userTr = $(window.parent.document).find("#divHcpList table tbody tr");    if(userTr.length > 0){        $(window.parent.document).find("#Clean_up").attr('disabled','');    }else{        $(window.parent.document).find("#Clean_up").attr('disabled','disabled');    }}           



这个方法在运行超过100条数据的时候浏览器会报错,
火狐下:A script on this page may be busy, or it may have stopped responding. You can stop the script now, open the script in the debugger, or let the script continue.
请教各位大侠,这个问题应该怎么解决,每次数据必然超过200条?


[解决办法]
每执行一定数据量停一下,让JS休息一下,再执行,这样可以避免这种提示产生(用window.setTimeout,延迟一下)
[解决办法]
确实建议使用楼上的方法,其实js的主要功能还是主要与用户的交互。而且大部分浏览器中UI和js的执行线程是头一个线程,所以长时间的js执行(大概100ms吧)就会导致UI无响应

读书人网 >JavaScript

热点推荐