首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

关于javascript自动计算占CPU的有关问题

2012-02-02 
关于javascript自动计算占CPU的问题我做的个考评表有用javascript自动计算的部分,但是我发现运行这个网业

关于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   type=text   name= "secondBox "   value= " "   onFocus= "startCalc(); "   onBlur= "stopCalc();sum(); ">   =  


<input   type=text   name= "thirdBox "> </div>
<input   type= "button "   value= "append "   onclick= "app();sum() "> <span   style= "margin-left:262px "> sum: <input   type= "text "   name= "totalBox "   value= "0 "> </span>
</form>
<p>   </p>
</body>
</html>




[解决办法]
window.onload = function startCalc(){
interval = setInterval( "calc();sum(); ",1);
}
改成
window.onload = function startCalc(){
interval = setTimeout( "calc();sum(); ",10);
}
这样看看
[解决办法]
如果希望像原来那样一直计算可以这样改
window.onload = function startCalc(){
interval = setInterval( "calc();sum(); ",100);
}
把时间改大
[解决办法]
不就自动计算嘛。需要用到setInterval吗?
你用键盘事件不好啊。onkeypress.onkeydown.onkeyup 都比setInterval好使阿。

PS:LZ分真多。
[解决办法]
setInterval( "calc();sum(); ",1);
的问题啊!

热点排行