delaydomcontentloaded

How to fire code again one additional time after DOMContentLoaded + delay?


document.addEventListener('DOMContentLoaded', () => {
    // do something
}

window.load isn't late enough, so I'd like to deliberately delay the second check/firing for say, 5 seconds.

I don't simply want to replace DOMContentLoaded with setTimeout, either. I want the code to execute immediately upon DOMContentLoaded, but also just a single time more after 5 seconds (as opposed to continually checking every 5 seconds).


Solution

  • I want the code to execute immediately upon DOMContentLoaded, but also just a single time more after 5 seconds (as opposed to continually checking every 5 seconds).

    Put the // do something part of the code into a function, and then use

    document.addEventListener('DOMContentLoaded', () => {
      do_something();
      setTimeout(do_something, 5000);
    });