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

80
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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