how restricted is recursion in javascript? -
I think browsing has been stopped by dough codes to stop the tube but this is:
function print (item) {document.getElementById ('output'). InnerHTML = document.getElementById ('Output'). InnerHTML + item + '& lt; Br / & gt; '; } Function recurrence (myInt) {print (myInt); If (int and lieutenant; 10) {for (i = 0; i & lt; = 1; i ++) {recurs (myInt + 1); }}} produces:
0 1 2 3 4 5 6 7 8 9 10 10 And no, when I do, I do not get older old disturbances:
function myInt {print (myInt); If (int and lieutenant; 10) {for (i = 0; i & lt; = 1; i ++) {var x = myInt + 1; SetTimeout ("repetition (" + x + ")"); }}} Am I forgetting something or is it how do you reproduce in JS? I remember that you need to call the method for each child. You are using a global variable in the form of loop counters, this is the reason that this is the only complete ending for the most endless call when you come back from that call, the counter is already loop for all other loops And beyond.
If you create a local variable:
function recurrence (int) {print}; If (int and lieutenant; 10) {for (var i = 0; i & lt; = 1; i ++) {recurring (int + 1); }}} Output is the same number of times when you use timeouts, global variables do not cause the same problem because the recursive call queued and Are executed later, when you exit the loop.
Comments
Post a Comment