0

I know i can use setTimeout but my software has some limitation so I have to use a workaround. I found this online that is supposed to work as a delay function:

function sleep(milliseconds) {
 var start = new Date().getTime();
 for (var i = 0; i < 1e7; i++) {
   if ((new Date().getTime() - start) > milliseconds){
 break;
}
}
}

Because im new to Js i do not know how to call this function. I am trying to do something like this:

sleep(2000) {
...
}

But the above is not working. Can someone please show me how to run the function. Thank you.

Note my question is different because I can't use the setTimeout function.

user982853
  • 2,362
  • 12
  • 49
  • 76
  • `sleep(2000);` should work – Banzay May 22 '19 at 15:18
  • 1
    Remove the curly brackets `sleep(2000)` – Amin Jafari May 22 '19 at 15:18
  • 1
    keep in mind that this is a blocking process that will freeze your system from executing anything else for the time you set as an argument – Antonis May 22 '19 at 15:20
  • Question for you and a warning to any future visitors - are you *really* sure you can't use setTimeout? This method is a truly horrendous way to achieve what you're trying to do, locking up the user's browser while you wait for however long. If it's at all possible, you should find an alternative way to do this. – Hecksa May 22 '19 at 15:24
  • Possible duplicate of [What is the JavaScript version of sleep()?](https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep) – lucifer63 May 22 '19 at 15:33
  • @Antonis is there a script that will delay without blocking all other system scripts besides setTimeout. I checked with support and they don't allow setTimeout. The script above now works using sleep(2000); but i need something that doesn't block all other scripts. Im trying to delay to allow another background script to finish running, then trigger my script. – user982853 May 22 '19 at 15:54
  • Well you can use `async`. But since you say that you can't even use `setTimeout()` you might have issues. Anyway, take a look a that --> https://stackoverflow.com/questions/33289726/combination-of-async-function-await-settimeout/33292942 – Antonis May 23 '19 at 07:55

3 Answers3

0

Just do the following:

sleep(2000);

The function is designed to keep the scripting running the function for X milliseconds, then the function finishes and the script keeps running. Any code that you want to run after the sleep function will just have to be on the next line.

Also, you don't have to do this, but here's the code formatted properly:

function sleep(milliseconds) {
    var start = new Date().getTime();

    for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds) {
            break;
        }
    }
}
VirxEC
  • 739
  • 9
  • 19
  • Apparently this script stops all other script from running. Is there an alternative function that just delays the running of a block of code while allowing the background scripts to continue to run? Im trying to delay my script so that a background script can finish running before mine executes. I can't use setTimeout which is a limitation to my software. – user982853 May 22 '19 at 16:05
  • yes, the setTimeout() JavaScript function. https://www.w3schools.com/jsref/met_win_settimeout.asp – VirxEC May 22 '19 at 16:40
0

That function doesn't look great. I would recommend using a promise and .then():

function sleep(time) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve()
    }, time)
  }
}

You can call this function like this:

await sleep(2000)

or

sleep(2000).then(() => {
  // rest of code
})

NONETHELESS, going back to your question, you would call the function like this:

sleep(2000)
// rest of code
Kobe
  • 5,539
  • 1
  • 9
  • 32
0

You could use requestAnimationFrame to emulate this.

function sleep(milliseconds, callback) {
    requestAnimationFrame(function() {
        var start = performance.now();

        for (var i = 0; i < 1e7; i++) {
            if ((performance.now() - start) > milliseconds) {
                return callback();
            }
        } 
    })
}

You would call it like this:

console.log(1);
sleep(2000, () => console.log(3));
console.log(2);

function sleep(milliseconds, callback) {
  requestAnimationFrame(function() {
    var start = performance.now();

    for (var i = 0; i < 1e7; i++) {
      if ((performance.now() - start) > milliseconds) {
        return callback();
      }
    }
  })
}

console.log(1);
sleep(2000, () => console.log(3));
console.log(2);
Paul Cosma
  • 182
  • 1
  • 10
  • Does this stop all other scripts? Background scripts? What im trying to do is have my script trigger after all other scripts have run. If this halts everything then it doesn't re-order the execution it just holds them all. – user982853 May 22 '19 at 20:35