简体   繁体   中英

Sleep() not working as expected

In my code, I'm trying to put a certain delay before continuing to the rest of the code. Pretty basic. I'm using a custom sleep function because javascript's native sleep function is not working at all. I'm actually working in app script in google spreadsheets so maybe that's why. But the following code is in the <script> tag of the html file in spreadsheet app script.

Anyway, when I use sleep() , it is being executed before the setTimeout

function get_ids(db){

    window.alert("before window.ids is saved?");
    google.script.run.withSuccessHandler(getIdsFromAppscript).getIdsFromModel(db); 
    //this returns a result to getIdsFromAppscript but the following code doesn't wait 
    //for the result to be returned so I want to use sleep before the ids 
    //variable is returned by get_ids


    setTimeout(function(){
        window.alert("checking if ids is saved after 10s?");
        window.alert("In timeout ids="+window.ids);
        var ids= window.ids; //is non empty
    },10000);

    sleep(10000);

    var ids= window.ids;
    window.alert("after wait");
    window.alert("after sleep ids="+ids); //is empty but should be non empty

    return ids; //=window.ids , Need to return a non-empty result

}

function getIdsFromAppscript(result){
window.ids=result;

}

and the sleep function:

function sleep(ms) {
  var start = new Date().getTime(), expire = start + ms;
  while (new Date().getTime() < expire) { }
  return;
}

Current Order of output based on window.alert() :

1) before window is saved?
2) after sleep
3) after sleep ids=  //basically empty which shouldn't be the case
4) checking if ids is saved after 10s?
5) In timeout ids= [.....] //non empty list

However, my desired output order is:

1) before window is saved?
2) checking if ids is saved after 10s?
3) In timeout ids= [.....] //non empty list
4) after sleep
5) after sleep ids= [...] //should be a non empty list 

The reason why I'm writing setTimeout is to show that after 10 seconds, the result is being stored in window.ids however even after I give a sleep for 10 seconds, or even 30 seconds, I can't get the result from window.ids.

What exactly am I doing wrong here? Thanks in advance~

Java Script, especially through the V8 engine does not sleep. Sleeping causes the entire thread that JavaScript runs on to stop, which breaks the whole point of asynchronocy. setTimeout() only waits to run the function you push into it for the time you also put into it. It doesn't stop the rest of the executions, and whatever happens first will happen first.

If it's important to you that something happens in order, always, then you need to use callbacks or promises.

An example in code could be:

function doTimeout(ms) {
    setTimeout(function(){
      window.alert("checking if ids is saved after 10s?");
      window.alert("In timeout ids="+window.ids);
      var ids= window.ids; //is non empty
    },ms);
}

function sleep(ms, callback) {
  var start = new Date().getTime(), expire = start + ms;
  while (new Date().getTime() < expire) { }
  callback(ms);
}

sleep(10000, doTimeout);

Javascript is single threaded. You must return from your code for scripts in other threads to execute. Script in other threads includes functions to handle a timeout event, functions called when promises are kept or fail, and call back functions provided for asynchronous requests made using an XMLHttpRequest object.

Writing a function and calling it sleep() does not change this. You could have called it waitingForGodot() for all the difference it would make. What the code you provided does is to spend a lot of time looping in the thread it was called in. It does not relinquish control and blocks all other scripts from executing. If it goes on for long enough my browser will ask me if I wish to abort the (as in your) script.

I have included two examples below showing that your sleep function blocks the entire Javascript engine. When I use your sleep function, the interval function does not get executed even though I have set an interval of 100 ms and the output is delayed by 10 seconds. However, in the second example the output does get printed immediately at the correct interval. This shows your sleep function is blocking the entire execution engine and that explains why your ids array is empty.

 function sleep(ms) { var start = new Date().getTime(), expire = start + ms; while (new Date().getTime() < expire) {} return; } function get_ids() { document.write("before window.ids is saved?" + "<br>"); var counter = 0; setInterval(function() { while (counter < 100) { document.write("checking if ids is saved after 10s?" + "<br>"); counter = counter + 1 } }, 100); sleep(10000); documen.write("after wait"); } document.write("Start"); get_ids() document.write("End"); 

In this example I have commented out your sleep function and as expected the output gets printed every 100 ms:

 function sleep(ms) { var start = new Date().getTime(), expire = start + ms; while (new Date().getTime() < expire) {} return; } function get_ids() { document.write("before window.ids is saved?" + "<br>"); var counter = 0; setInterval(function() { while (counter < 100) { document.write("checking if ids is saved after 10s?" + "<br>"); counter = counter + 1 } }, 100); //sleep(10000); documen.write("after wait"); } document.write("Start"); get_ids() document.write("End"); 

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