简体   繁体   中英

How to use setTimeout in a for loop

I'm building a Minecraft themed bot in discord.js and there is a command I wrote which basically starts mining for resources and then it waits for some time because in real life, mining stone will take some time in Minecraft and it also depends on the type of pickaxe. So what I wanted to do was that every time after you mine a block, it waits for some time according to the speed of your pickaxe and after that only, it runs again and for this, I thought of using the setTimeout() function. But I ran into a problem. The problem was that while the setTimeout() was running, it didn't stop there but continued on and executed the code after the setTimeout() and once the setTimeout() was done, it executed the code in the setTimeout() . I've researched a bit on how to do this for a little time but none of them seem to work for what I think. My code is like this =>

 async execute(interaction) { // It gets the pickaxe details const pickaxe = require('../${pickaxeId}.js') // This gets the time to mine stone, coal and iron from the pickaxe file const timeToMineStone = pickaxe.stone const timeToMineCoal = pickaxe.coal const timeToMineIron = pickaxe.iron const minableBlocks = ['stone', 'coal', 'iron'] var miningTime // It picks one block randomly from the minableBlocks and depending on it, it assigns a value to the miningTime variable // Code for picking the random block for (var mined = 0; mined <= 100; mined++) { setTimeout(() => { console.log('Time Out,') }, miningTime) // Rest of the code should only be executed after the setTimeout() } }

But what happens here is that the rest of the code written after the setTimeout() executes first and then after the timeout, it logs 'Time Out.' to the console. Can anyone help me on how to use setTimeout() properly? Any help would be appreciated.

Your sleep function does not work because setTimeout does not (yet?) return a promise that could be await ed. You will need to promisify it manually:

function timeout(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
async function sleep(fn, ...args) {
    await timeout(3000);
    return fn(...args);
}

your function will become

async function execute(interaction){
  // It gets the pickaxe details
  const pickaxe = require('../${pickaxeId}.js');
  // This gets the time to mine stone, coal and iron from the pickaxe file
  const timeToMineStone = pickaxe.stone
  const timeToMineCoal = pickaxe.coal
  const timeToMineIron = pickaxe.iron
  const minableBlocks = ['stone', 'coal', 'iron']
  var miningTime
  // It picks one block randomly from the minableBlocks and depending on it, it assigns a value to the miningTime variable
  // Code for picking the random block
  for (var mined = 0; mined <= 100; mined++) {
    timeout( () => {
      console.log('Time Out!')
    }, miningTime)
    // Rest of the code should only be executed after the setTimeout()
  }
}

A similar question is asked here . Copied from this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM