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

274
Visualizações
How can I make a function sleep with Express?

I was trying to recreate a popular gambling game called crash (like on the website roobet.com/crash). I needed to build a backend server so the current multiplier could be sent to all the players. And I've came up with this code.

function set_result() {
    if (!initialized) {
        init()
        initialized = true
        crashed = false
    }
    curr_time = new Date().getTime()
    curr_point = Math.pow(e, (curr_time - start_time) / 1000).toFixed(2)
    if (curr_point === crash_point) {
        curr_point = 0
        crashed = true
        initialized = false
    }
}

setInterval(set_result, 1)

io.on('connection', async socket => {
    async function send_result() {
        if (crashed) {
            socket.emit('message', {crash: `Crashed at ${crash_point}`})
            return
        }
        socket.emit('message', {crash: curr_point, time: (curr_time - start_time) / 1000})
    }
    setInterval(() => send_result(), 100)
})

It works perfectly. The result is getting calculated in the background and is being sent to all the connected players. But after every game in crash, there is a few second delay where the players can place their bets. To do that i need to make the set_result() function sleep after curr_point is equal to crash_point. How can i make the set_result() function sleep without stopping the whole server?

about 4 years ago · Juan Pablo Isaza
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