0

My work has me working on a React App and the code has an onload event listener

The load event listener is supposed to fire a function to create cookies on load but instead never fires

For example:

window.addEventListener('load', function(){
      console.log('Yo 2');
});

window.addEventListener('load', console.log('Yo'))

Yo prints before Yo 2

What the timing difference and why?

This is a simplified form of the Bug, we thought it was a cookie issue but the cookie is never created so we had to back track and realize the event listener isn't firing correctly on some machines.

Our actual call back function is async, could this be why?

Jon Gan
  • 29
  • 5
  • 1
    Just like your duplicated links above would tell you its difference is between calling a function VS passing a function's reference as argument (callback). For your particular example "Yo 2" would log first if you immediately called it as IIFE: You can try this: `..., (function(){console.log('Yo 2');})())` – Aleksandar Jan 27 '23 at 04:12

0 Answers0