javascript encapsulates the method of addLoadEvent to realize page loading and executing multiple functions at the same time

  • 2021-07-04 18:13:19
  • OfStack

In this paper, an example of javascript encapsulation addLoadEvent implementation page loading and executing multiple functions at the same time. Share it for your reference, as follows:

If you want to execute multiple functions at the same time, you can put them into an array, then loop the array and execute it in the onload event, or use another convenient function, addLoadEvent:


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
   } else {
    window.onload = function() {
         if (oldonload) {
          oldonload();
         }
      func();
    }
   }
 }

For more readers interested in JavaScript related content, please check the topics on this site: "Summary of Common Function Skills of JavaScript", "Summary of Switching Special Effects and Skills of JavaScript", "Summary of Search Algorithm Skills of JavaScript", "Summary of Animation Special Effects and Skills of JavaScript", "Summary of Error and Debugging Skills of JavaScript", "Summary of Data Structure and Algorithm Skills of JavaScript", "Summary of Traversal Algorithm and Skills of JavaScript" and "Summary of Mathematical Operation Usage of JavaScript"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: