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

183
Visualizações
Execute a function for x time

I'm looking for execute a function on nodejs with express for an exact time, and if the time have pass but the function don't end, the function stop.

I tried with setTimeOut. But it just works for execute a function after "x" time. I just need to execute the function for "x" time.

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

0

You could set a Boolean variable and change its value after "x" of time, while checking the same variable in your function.


var Continue = true; // Our Boolean variable.
var x = 5000; // Execute the code for amount of milliseconds.

function canContinue(){
    return Continue;
}

var ourInterval = setInterval( function () {
    if(canContinue())
        console.log("Continue the code for one more second.")  
    else
        clearInterval( ourInterval )
}, 1000 );

setTimeout( function () {
    Continue = false;
}, x);

Hope that helps.

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