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

272
Views
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
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!