I have a recursive function reopen() which is supposed to loop indefinitely however after calling the function openWin() it stops for some reason.
Here is the code:
function openWin(i = 0) {
win[i] = window.open(url,'', 'width=200,height=200');
wins ;
}
function reopen() {
for(var i = 0; i < wins; i ) {
if(win[i].closed) {
openWin(i);
openWin(wins);
}
}
setTimeout(reopen, 10);
}
Any idea why this is happening and how to fix it?
EDIT: A lot of people seem to think that the problem is coming from calling openWin() twice, however it has the exact same problem when only calling it once
CodePudding user response:
You have an infinite loop. Each time you call openWin() it inrements wins. Since you call openWin() twice in the loop when win[i] isn't open, you'll add 2 to wins during some of the repetitions of the loop. So i < wins will always be true, because wins increases faster than i does.
function reopen() {
const wincount = wins;
for(var i = 0; i < wincount; i ) {
if(win[i].closed) {
openWin(i);
openWin(wins);
}
}
setTimeout(reopen, 10);
}
CodePudding user response:
For some reason the loop was having problems with calling a function, but by copying the code from openWin() and pasting it in directly it works now :)
