rando-idiot / Terminus.JS

A somewhat small incremental game played in a web terminal.
https://rando-idiot.github.io/Terminus.JS/
Creative Commons Zero v1.0 Universal
2 stars 1 forks source link

Sleep function #14

Closed Bleb1k closed 1 month ago

Bleb1k commented 1 month ago

Issue:

current stack overflow sleep function uses up 100% of CPU time to do it's job

Solution:

Use setTimeout function instead

while this is a viable solution, it has unwanted side-effects

better approach can be found there

additional syntax (async/await) just means that function can wait for certain time, and is useful for loading files and/or data

Examples:

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

async function start() {
  console.log("Execution...");
  for (let i = 0; i < 10; i++) {
    console.log(`${i} seconds passed`);
    await sleep(1000);
  }
  console.log("End of execution");
}

console.log("Preparing the program")
sleep(1000).then(start); // you are allowed to call async functions without await
// or without sleep
// start()
Bleb1k commented 1 month ago

whoa, the bots! @rando-idiot don't forget to report 'em before deleting

rando-idiot commented 1 month ago

i dont see bots, also im not even using the sleep function anymore since it was for an old thing so ill just delete it as its mostly bloat