1

I have a question:

window.addEventListener("visibilitychange", function(e) {
  console.log(window.uidd)
  window.uidd = window.uidd || (new Date).getTime() + Math.random()
  console.log(window.uidd)
})

But if you open console and first time you get:

undefined
124124214124124.124

window.uidd // undefined

Why is that so last string is undefined?

msanford
  • 11,803
  • 11
  • 66
  • 93

1 Answers1

1

I'll have to make this an answer only because I cannot attach screen shots to comments, but I do not see this behaviour.

  1. I confirm that the window.uuid variable does not exist on window.
  2. I add the event listener, and get the expected first undefined from registering it.
  3. Then I click out of the tab (the first visibilitychange event), and get the second undefined and the value set for window.uuid, both logged from the listener.
  4. I click back into the tab, firing the second visibilitychange event, and get the uuid logged twice.
  5. Typing window.uuid into the console returns the global uuid value.

enter image description here

I suspect the confusion may lay in the way you are attaching the event listener.

EDIT:

With the following test page:

<html>
<head>
    <script>
        window.addEventListener("visibilitychange", function(e) {
            console.dir(window);
            console.log(window.uidd)
            window.uidd = window.uidd || (new Date).getTime() + Math.random()
            console.log(window.uidd)
        })
    </script>
</head>
<body></body>
</html>

I do see this behaviour. You need to access the property using this.uuid, rather than window.uuid, even though simply typing window will show the .uuid property.

Curiously, when I replace your test with this static assignment:

window.addEventListener("visibilitychange", function(e) {
    window.foo = window.foo || "bar";
})

it does work, and I can see the expected value of window.foo directly in the console.

I, too, am curious, and asked the Chrome team. I'll update this answer if someone from there doesn't answer directly.

msanford
  • 11,803
  • 11
  • 66
  • 93
  • If you added listener in console yes that work. Try added listener in script block. After that when listener already initilize window.uidd must have value, but u can checked it is not be Truth – Юра Панарин Sep 25 '17 at 21:00
  • @ЮраПанарин Updated. – msanford Sep 25 '17 at 21:22
  • 1
    it is so strange behavior – Юра Панарин Sep 25 '17 at 21:27
  • @ЮраПанарин I agree; my apologies, that is why I did not understand your interesting question at first. I did not expect this behaviour either. – msanford Sep 25 '17 at 21:28
  • it is my English skills :) – Юра Панарин Sep 25 '17 at 21:30
  • @msanford You [voted to reopen](https://stackoverflow.com/review/reopen/17440408) [a duplicate in MATLAB](https://stackoverflow.com/questions/46407958/indexing-rules-for-extracting-sub-arrays-from-a-cell-array). You were wrong, and deciding whether the dupe is relevant needs domain knowledge. Please chose "skip" in the review queues in case of duplicates unless you are absolutely sure. Feel free to flag this comment as "no longer needed" once you've read it. Thanks. – Andras Deak -- Слава Україні Sep 26 '17 at 09:46