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

139
Visualizações
Will code after a jQuery ready() be guarantied to run before the ready function?

As titled!

ready() specifies that it loads after the DOM has been initialized and is ready for JS execution. However, I'm unsure of how that interacts with the JS itself!

If I have some JavaScript with the general format:

let functocall = () => {
    alert("Bad ending!");
};

// ... 

$(document).ready(() => {
    functocall();
});

// ...

functocall = () => {
    console.log("Safe!");
};

(note that the JS is loaded via a <script ... defer> in the head)

In this example, would it ever be possible for the document's ready function to be called and result in the alert() being called? On the examples I've tried, and the code I'm actually running, it never does, but I'm unsure if there's some edge case where it'd occur due to how the timing lines up.

I'd prefer it this way due to the following constraints:

  • functocall cannot be initialized with the other values
  • The ready() call being above the update is for code organization purposes (i.e. the lower section is a long chunk of code, and would be messy above the ready())
  • This JavaScript file is loaded with defer, to allow the libraries to load in first

I'd imagine I could just put the ready() into a function, and then call it and pass in the function, but I just personally think it'd look a bit worse that way, so I wanted to see if this was possible instead.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

No. We don't know when the whole code is executed. It could be loaded after the document is ready, so the ready promise is resolved and the code will execute immediately.

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