Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

277
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda