Why does my function run out of memory if I don't use setTimeout(..., 0)?
I can see what's happening:
if the loop number is greater than 459, the error occurs, and without a timeout, the output moves at light-speed.
However, I don't understand what's actually going on here...
why does the "out of memory" error occur, and what does a zero timeout-delay do to correct the problem?
I can see what's happening:
if the loop number is greater than 459, the error occurs, and without a timeout, the output moves at light-speed.
However, I don't understand what's actually going on here...
why does the "out of memory" error occur, and what does a zero timeout-delay do to correct the problem?
Code:
<html> <head> <title>-</title> <script type="text/JavaScript"> var i, n, stop; function Recurse(oForm){ i++; oForm.progress.value = i; oForm.percentage.value = Math.floor(((i / n) * 100)) + "%"; i < n && +stop != 1 ? [COLOR=red]setTimeout(function (){Recurse(oForm);}, 0)[/COLOR] : controlUpdate("Complete"); } function controlUpdate(callString){ oForm = document.RecTest; switch(callString){ case "Start" : n = new Number(oForm.test.value); if(isNaN(n) || n < 1){ alert("loop value must be a number greater than 0. "); } else{ i = 0; stop = false; oForm.cancel.disabled = 0; oForm.start.disabled = 1; oForm.test.disabled = 1; Recurse(oForm); } break; case "Cancel" : stop = true; oForm.rs.disabled = 0; oForm.cancel.disabled = 1; break; case "Complete" : oForm.rs.disabled = 0; break; case "Reset" : oForm.cancel.disabled = 1; oForm.start.disabled = 0; oForm.test.disabled = 0; oForm.rs.disabled = 1; break;} } </script> <style type="text/css"> form{ text-align:center } input{ text-align:center;vertical-align:middle } label{ padding-left:10px;padding-right:5px } </style> </head> <body> <form name="RecTest" onreset="controlUpdate('Reset')"> <label>i :</label><input name="progress" size="10" readonly> <label>% :</label><input name="percentage" size="10" readonly> <br><br> <label>loop :</label><input name="test" value="500" size="10" maxlength="5"> <input name="start" type="button" value="Start" onclick="controlUpdate('Start')"> <input name="cancel" type="button" value="Cancel" disabled onclick="controlUpdate('Cancel')"> <input name="rs" type="reset" disabled> </form> </body> </html>
Comment