-4

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

  • Why do you call `openWin()` twice? – Barmar Aug 29 '22 at 21:07
  • Protip: Stating that there's no reason a repeatable thing happens is a sure way to end up looking silly. :) Please see [ask], then revise your post title to ask a clear, specific question. – isherwood Aug 29 '22 at 21:07
  • If that is the case wouldn't it be fixed by increasing the delay in `setTimeout()`, because i have tried that and it didn't fix it – Garrett Murray Aug 29 '22 at 21:09
  • 2
    That's not really "recursive", also. You didn't post what "wins" is, or what "url" is, or why you're doing this in the first place. – Pointy Aug 29 '22 at 21:09
  • 1
    Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Aug 29 '22 at 21:10
  • @Barmar to open 2 windows – Garrett Murray Aug 29 '22 at 21:10
  • @Pointy Why is it not recursive? Also wins is an array of windows and url is just a url blank url for the windows. The point is to open 2 new windows everytime one of the windows is closed – Garrett Murray Aug 29 '22 at 21:16
  • 1
    That's using JS for evil, and the browser might be preventing that. – mbojko Aug 29 '22 at 21:18
  • 2
    It's not recursive because there will only be one instance of the function on the stack at any time. It *looks* recursive, but it isn't; the `setTimeout()` call schedules the call for the future, and by the time that call happens the original call is finished. The calls to the function do not rely on any returned result from a previous call. – Pointy Aug 29 '22 at 21:45

1 Answers1

2

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);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612