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

79
Views
JavaScript sleep function sequence?

I was making a discord bot with JavaScript. I'd like to make cooltime when player works bot.

First, I set waiting function.

function sleep(ms) {
    return new Promise((r) => setTimeout(r, ms));
}

And,

if (mine_cooltime === 0) {
    mine_cooltime = 1;

    const msgRef = await message.reply({embeds: [embed_mine_stone]}); // Don't care this

    sleep(user.mine_time)
    .then(() => msgRef.edit({embeds: [embed_mine_stone_end]})) // Don't care this
    .then(() => saveUser = {id, name, wood : user.wood, stone : user.stone + 3, iron : user.iron, coal : user.coal, gold : user.gold, diamond : user.diamond,
        findWood : user.findWood, findStone : 1, findironOre : user.findironOre, findCoal : user.findCoal, findGold : user.findGold, findDiamond : user.findDiamond, user.mine_time : user.mine_time
        })
    .then(() => fs.writeFileSync(filePath, JSON.stringify(saveUser)))
    .then(() => mine_cooltime = 0);
}

This is a code which mines stones.

if (wood_cooltime === 0) {
    wood_cooltime = 1;

    const msgRef = await message.reply({embeds: [embed_wood_cut]}); // Don't care this
    sleep(user.wood_time)
    .then(() => msgRef.edit({embeds: [embed_wood_cut_end]})) // Don't care this
    .then(() => saveUser = {id, name, wood : user.wood + 5, stone : user.stone, iron : user.iron, coal : user.coal, gold : user.gold, diamond : user.diamond,
        findWood : 1, findStone : user.findStone, findironOre : user.findironOre, findCoal : user.findCoal, findGold : user.findGold, findDiamond : user.findDiamond, user.mine_time : user.mine_time
        })
    .then(() => fs.writeFileSync(filePath, JSON.stringify(saveUser)))
    .then(() => wood_cooltime = 0);
}

And this is a code which cuts woods.

Both "mine_time" and "wood_time" are 5000.

If I operate mining code 3 seconds after operate cutting code, At first 2 seconds after mining code were finished, "wood" was increased. But when mining code finished, 'wood" was same and only "stone" was increased.

Why is this happening, and how can I fix it?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This happens because you assign the modified user object to saveUser, which means user still has the old values. So if a few seconds later the other code also creates a modified object, it will make the modification from the original object, and not from the modified version.

I would suggest to forget about the saveUser variable and just assign to user, and use that in JSON.stringify as well.

Maybe you need the user object to be immutable, but if this is not a requirement, you don't even need to create an entirely new object with an object literal. Instead, you could just mutate the existing object with user.wood += 5 and user.stone += 3. But again, this will mutate the user object, which maybe will not work with your state management framework.

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