关于javascript嵌套循环的效率问题,请高手来看看!!!!
function SelectAll(ctr)
{
var dg = document.getElementById( "dgdMaterialRequest ");
for(var j = 2; parseInt(j) <= dg.rows.length; j++)
{
if(j.toString().length==1)
{
j= "0 " + j.toString();
}
var chk = document.getElementById "dgdMaterialRequest_ctl "+j+ "_cbIsSelected ");
if(chk != null && chk.disabled != true)
{
chk.checked=ctr.checked;
CheckBoxSelected(chk,j);
}
}
}
function CheckBoxSelected(Ctr,rowID)
{
if(rowID.toString().length==1)
{
rowID= 'ctl0 '+(rowID);
}
else
{
rowID= 'ctl '+(rowID);
}
var sumDrawQuantity=0;
var materialID=document.getElementById( "dgdMaterialRequest_ "+rowID+ "_lblMaterialID ").innerText;
var waitForDrawQuantity=document.getElementById( "dgdMaterialRequest_ "+rowID+ "_lblWaitForDrawQuantity ");
var inventoryQuantity=document.getElementById( "dgdMaterialRequest_ "+rowID+ "_lblInventoryQuantity ");
var drawQuantity=document.getElementById( "dgdMaterialRequest_ "+rowID+ "_txtDrawQuantity ");
var reservedFigures=document.getElementById( "dgdMaterialRequest_ "+rowID+ "_hdnReservedFigures ");
if(Ctr.checked)
{
var rowCount=document.getElementById( "hdnRowCount ").value;
for(var i=2; i < parseInt(rowCount)+2; i++)
{
if(i.toString().length==1)
{
i= '0 '+i.toString();
}
var strMaterialID=document.getElementById( "dgdMaterialRequest_ctl "+i+ "_lblMaterialID ").innerText;
var strDrawQuantity=document.getElementById( "dgdMaterialRequest_ctl "+i+ "_txtDrawQuantity ").value;
if(strMaterialID==materialID && strDrawQuantity != " " )
{
sumDrawQuantity=parseFloat(sumDrawQuantity) + parseFloat(strDrawQuantity);
}
}
if(parseFloat(waitForDrawQuantity.innerText) <= parseFloat(inventoryQuantity.innerText) - parseFloat(sumDrawQuantity))
{
drawQuantity.value=waitForDrawQuantity.innerText;
}
else
{
if(parseFloat(inventoryQuantity.innerText)==0)
{
alert(JS_MSG_MRF5);
Ctr.checked=false;
drawQuantity.value= " ";
return;
}
else if(parseFloat(inventoryQuantity.innerText) - parseFloat(sumDrawQuantity) < 0)
{
alert(JS_MSG_MRF6);
Ctr.checked=false;
drawQuantity.value= " ";
return;
}
else
{
if( drawQuantity.value == " ")
{
drawQuantity.value=round(parseFloat(inventoryQuantity.innerText) - parseFloat(sumDrawQuantity),reservedFigures.value);
}
}
}
}
else
{
drawQuantity.value= " ";
}
}
--两次for循环导致在大数据量的情况下 ,效率极低,各位帮忙看看是否可以优化?
[解决办法]
....
提供代码时, 需要提供测试数据....
[解决办法]
个人感觉,写出代码的同时,给点注释是个好习惯。。。。。