以下函數正在使用我的計算機上100%的CPU核心。有沒有一種方法可以將其重寫爲非遞歸?這會解決它還是因爲我的CPU太爛?其他人是否在他們的電腦上看到相同的性能問題?如何優化這個遞歸函數,它調用setTimeout()
代碼:
<html>
<head>
<script type="text/javascript" src="jquery-1.7.1.js"></script>
<script type="text/javascript">
function timeMsg(n,max,delay)
{
writeToLog(n + "th: &#" + n,n);
var temp = n + 1;
if(n < max){
var t=setTimeout("timeMsg(" + temp + "," + max + "," + delay + ")",delay);
}
}
function writeToLog(text,n){
$("#log").html($("#log").html() + text + "<br/>");
//autoscrolling: doesn't work...'
}
</script>
</head>
<body>
<form>
<input type="button" value="Display alert box in 3 seconds" onClick="timeMsg(0,100000,100)" />
</form>
<div id="log"></div>
</body>
謝謝:)真正幫助它。 – user269334 2011-12-29 20:00:35