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

160
Visualizações
How can I construct this server backend route?

I'm working on a fullstack project where I need a way to effectuate the following scenario:

Upon a user submit event:

  1. A request is posted to the backend
  2. When the server receives the req., it begins executing some function
  3. The backend should run said function on a loop/indefinitely until the backend receives a subsequent request which instructs the server to cease executing the function after the function completes its current iteration.

I know there must be a way to work this out, but I haven't been able to figure out how to condense my problem/question into something I can google search my way through.

The following will hopefully help to convey what I'm trying to accomplish:

let run_function = false;

app.post('/start', (req, res) => {
  run_function = true;
  while (run_function)
    the_function(req.body);
}

app.get('/stop', (req, res) => {
  run_function = false;
}

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

0

Seems like a job for setInterval() / clearInterval()

let handle

// Default 200ms interval
const startLoop = (body, interval = 200) => {
  clearInterval(handle) // stop any previous loops
  handle = setInterval(the_function, interval, body)
}

app.post("/start", (req, res) => {
  startLoop(req.body)
  res.send("Started")
})

app.post("/stop", (req, res) => {
  clearInterval(handle)
  res.send("Stopped")
})
about 4 years ago · Juan Pablo Isaza Relatório

0

let LoopInterval = null;
    
app.post('/start', (req, res) => {
    LoopInterval = setInterval(() => {
        the_function(req.body);
    }, 0);
});
    
app.get('/stop', (req, res) => {
    clearInterval(LoopInterval)
});
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