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

91
Visualizações
My nested functions aren't working correctly

I've never used nested functions before, and this seemingly simple task is giving me trouble. When I run this code, only the first function works, and the second is completely unresponsive (but doesn't give me an error message). What am I doing wrong?

function toggleMobileMenu() {
    setTimeout(function showMobileMenu() {
        var x = document.getElementById("content");
        if (x.style.display === "none") {
            x.style.display = "block";
        } else {
            x.style.display = "none";
        }
    }, 1000);
    function toggleClasses() {
        var element = document.getElementById("myNav");
        element.classList.toggle("fixed");
        var element = document.getElementById("overlay");
        element.classList.toggle("fixed");
        var element = document.getElementById("site-header");
        element.classList.toggle("fixed");
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As @rayhatfield mentions in the comments you need to invoke the function so that it executes.

If the function is defined inside another function (nested) like it is in this case, but it is not invoked or called, then it wont execute.

function toggleMobileMenu() {
    
    setTimeout(function showMobileMenu() {
        var x = document.getElementById("content")
        if (x.style.display === "none") {
            x.style.display = "block"
        } else {
            x.style.display = "none"
        }
    }, 1000)
    
    function toggleClasses() {
      var element = document.getElementById("myNav")
      element.classList.toggle("fixed")
      var element = document.getElementById("overlay")
      element.classList.toggle("fixed")
      var element = document.getElementById("site-header")
      element.classList.toggle("fixed")
    }
    
    toggleClasses() // <--- invoke the function
}

The first part works properly because setTimeout() makes a callback to the function passed to the parameters after a certain amount of milliseconds. And then nothing happens because you are just defining a function inside another function.

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