Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
Make sure two functions in different files run in the correct order?

So I have two functions in different files.

File 1 looks like this:

function 1() {
    do something
}
window.addEventListener('load', () => {
    1();
});

and file two looks like:

function 2() {
    do something
}
window.addEventListener('load', () => {
    2();
});

I want function 1 to run before function 2. Currently I do this by importing them like this;

<head>
   <script src="file1">
   <script src="file2">
</head>

The problem with this is it is quite fragile as if someone where to switch the imports then it would stop working, so I am asking if there is a better way of doing it to ensure that function 1 will be run before function 2(and no I can't put them in the same file).

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Remove the event listeners from the JS files and wire up the events in the html file.

<head>
  <script src="file1">
  <script src="file2">

  window.addEventListener("DOMContentLoaded", () => {
    1()
    2()
  });
</head>

Better yet, load up the scripts at the end of the <body> which will help with faster load times of the raw html.

<head>
<!-- not much here -->
</head>
<body>
<!-- html -->

  <script src="file1">
  <script src="file2">

  <script>
    window.addEventListener("DOMContentLoaded", () => {
      1()
      2()
    });
  </script>
</body>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!