日期:2014-05-16  浏览次数:20337 次

关于javascript自动计算占CPU的问题
我做的个考评表有用javascript自动计算的部分,但是我发现运行这个网业十分占资源,尤其是占CPU非常厉害,可能是没有释放的问题,下面是个简单的例子,当点表单触发时,高的时候能占40%以上,这还是个简单的例子,我做的那个比这个复杂多了,CPU占80%以上,请高手们帮忙看看,顺便给我讲讲这段javascript为什么会占CPU那么高.
代码如下:
<html>
<head>
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 ">
<title> +&nbsp;   = </title>
</head>
<script   type= "text/javascript ">
window.onload   =   function   startCalc(){
    interval   =   setInterval( "calc();sum(); ",1);
}
function   calc(){
    //alert( "yy ");
    fi=   document.getElementsByName( "firstBox ");
    //alert(fi[0].value)
    se   =   document.getElementsByName( "secondBox ");
    thi   =   document.getElementsByName( "thirdBox ");
    for   (i=0;i <fi.length;i++){
        thi[i].value=((fi[i].value)*1)+((se[i].value)*1);
    }
    //two   =   document.autoSumForm.secondBox.value;  
    //document.autoSumForm.thirdBox.value   =   (one   *   1)   +   (two   *   1);
}
function   stopCalc(){
    clearInterval(interval);
}
function   app(){
    o=document.getElementById( "aa ");
    str=o.innerHTML;
    //alert(str);
    o1=document.getElementById( "bb ");  
    //alert(o1.html);
    o1.innerHTML=   o1.innerHTML+ " </br> "+str;
}

function   sum(){//累加
    thi   =   document.getElementsByName( "thirdBox ");
    tot   =   document.getElementsByName( 'totalBox ');
    tot[0].value= "0 ";
    for(i=0;i <thi.length;i++){
        if(thi[i].value!= ' '){
            tot[0].value=tot[0].value*1+thi[i].value*1;
}
    }
}
</script>
<form   name= "autoSumForm "   id= "form1 ">
<div   id= "aa "> <input   type=text   name= "firstBox "   value= "5 "   onFocus= "startCalc(); "   onBlur= "stopCalc();sum(); ">   +  
<input   type=text   name= "secondBox "   value= " "   onFocus= "startCalc(); "   onBlur= "stopCalc();sum(); ">   =  
<input   type=text   name= "thirdBox "> <br> </div>
<input   type=text   name= "firstBox "   value= " "   onFocus= "startCalc(); "   onBlur= "stopCalc();sum(); ">   +  
<input   type=text   name= "secondBox "   value= " "   onFocus= "startCalc(); "   onBlur= "stopCalc();sum(); ">   =  
<input   type=text   name= "thirdBox "> <br>
<div   id= "bb "> <input   type=text   name= "firstBox "   value= " "   onFocus= "startCalc(); "   onBlur= "stopCalc();sum(); ">   +  
<input &nbs