javascriptdom-eventsnavigation-timing-api

How to wait for all values in window.performance.timing are defined


Currently I read the values in window.performance.timing within a window.addEventListener('load', function () {...}) function. however in safari there are values like loadEventEnd still being set to 0.

How to delay my call to window.performance.timing in the best way event based until this and other values are defined?


Solution

  • Do an additional delay in the load event.

    window.addEventListener('load', check)
    
    function check() {
      if (performance.getEntriesByType("navigation")[0].loadEventEnd && other_values()) {
        callToPerformanceTiming()
      }
      else setTimeout(check, 0); //put it back in queue
    }
    

    As long loadEventEnd is 0 the check is repeated.
    You can also check other_values() to be very shure
    it is passed before you do callToPerformanceTiming()