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

76
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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