'Why window.closed is set to true before the window is closed?

I am trying to use the answer at https://stackoverflow.com/a/48240128/583363.

Open a window with window.open and then start an interval to check if it is closed. The problem is the closed property gets sets to true without the user closing the window.

Here is the code that shows the problem.

$('#content').on('click', '.myShareButton', function () {
  const newWindow = window.open('https://google.com', '_blank', 'width=200,height=200,scrollbars=no,status=no');
  console.log('window.closed', newWindow.closed); // prints false as expected
  if (newWindow) {
    const timer = setInterval(function () {
      console.log('window.closed', newWindow.closed); // prints false until closed gets set to true times
      if (newWindow.closed) {
        clearInterval(timer);
        console.log('window.closed', newWindow.closed); // prints true
      }
    }, 1);
  }
  return false;
});

Output from chrome devtools

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source