Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

278
Views
¿Cómo puedo hacer que una función duerma con Express?

Intentaba recrear un popular juego de apuestas llamado crash (como en el sitio web roobet.com/crash). Necesitaba construir un servidor back-end para que el multiplicador actual pudiera enviarse a todos los jugadores. Y se me ocurrió este código.

 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) })

Funciona perfectamente. El resultado se calcula en segundo plano y se envía a todos los jugadores conectados. Pero después de cada juego que falla, hay un retraso de unos segundos en el que los jugadores pueden hacer sus apuestas. Para hacer eso, necesito hacer que la función set_result() duerma después de que curr_point sea igual a crash_point . ¿Cómo puedo hacer que la función set_result() duerma sin detener todo el servidor?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!