简体   繁体   中英

Pause javascript tag execution

I'm wondering if there is any way to "pause" script execution until a promise is resolved and only than continue execution of that specific script.(i know i can use .then() and in es6 i can even use await , but that's not what i'm asking). i can implement sleep function like that:

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

can i check on each second the promise status? or because js is single threaded this will actually freeze everything? any messy way to wait for a promise synchronously ?

Edit: My goal is to handle this situation:

function loadScript(src) {
    try {
        var parent = document.getElementsByTagName('head')[0] || document.documentElement;
        var httpRequest = new XMLHttpRequest();
        httpRequest.open('GET', src, false);
        httpRequest.send();
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.text = httpRequest.responseText;
        parent.appendChild(script);
    } catch (e) {}
}

this function is located inside a third party js , now i can hook the XMLHttpRequest object and intercept that call and make it async but this will mess up everything , any ideas?

You can do sleep like this:

function sleep(duration) {
  return new Promise(function(resolve, reject) {
    setTimeout(()=> { resolve(0) }, duration);
  })
}

And call sleep when you need with await sleep(1000); //miliseconds await sleep(1000); //miliseconds ;

You need this?

 function sleep(delay) { var start = new Date().getTime(); while (new Date().getTime() < start + delay); } sleep(2000) alert('Hello!') 

can i check on each second the promise status?

If it has to be sync then no, as long as your sync script is running no other code will be executed.

or because js is single threaded this will actually freeze everything?

If it is all sync then, yes it will freeze everything and the Promise won't resolve because the sync script that is checking the status of the Promise is blocking.

any messy way to wait for a promise synchronously ?

No, there is no sync way to wait for a Promise to resolve.

Promises to don't create multi-threading in the JavaScript context. Promises allow to make cooperative multitasking easier, especially with the introduction of await / async , but the code is still single threaded.

Is something like this you are looking for ? Sorry for that I didn't understand your problem perfectly though.

 var promise = new Promise((res, rej) => $.getJSON( "https://jsonplaceholder.typicode.com/todos/1", res, rej)) while(promise.status === 'Pending') { // this waits for the promise continue; } promise.then(console.log) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

通常,对于肮脏的黑客,您只需设置一个无限的时间间隔即可检查所需的内容,当它变为true时,请清除该时间间隔并调用任何函数以继续执行预期的执行。

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