How to know if window “load” event was fired already

后端 未结 7 2095
独厮守ぢ
独厮守ぢ 2020-12-05 12:27

I\'m writing a Javascript script. This script will probably be loaded asynchronously (AMD format).

In this script, I\'d like to do nothing important until the

7条回答
  •  暖寄归人
    2020-12-05 13:04

    Based on @CTS_AE's approach, I have put together a solution for envrionments where:

    • window.addEventListener('load', activateMyFunction); and
    • window.addEventListener('DOMContentLoaded', activateMyFunction);

    don't work.

    It requires a single character substitution (eg. from

    window.addEventListener('load', activateMyFunction);
    

    to

    window_addEventListener('load', activateMyFunction);)
    

    The function window_addEventListener() looks like this:

    const window_addEventListener = (eventName, callback, useCapture = false) => {
    
      if ((document.readyState === 'interactive') || (document.readyState === 'complete'))   {
    
        callback();
      }
    }
    

提交回复
热议问题