Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda