简体   繁体   English

暂停执行JavaScript标记

[英]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). 我想知道是否有任何方法可以“暂停”脚本执行,直到承诺被解决为止,而不仅仅是继续执行该特定脚本。(我知道我可以使用.then(),在es6中我甚至可以使用await,但是那不是我要的)。 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? 还是因为js是单线程的,这实际上将冻结所有内容? 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? 这个函数位于第三方js内部,现在我可以钩住XMLHttpRequest对象并拦截该调用并使它异步,但这会弄乱一切,有什么想法吗?

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 并在需要时调用sleep并await sleep(1000); //miliseconds 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? 还是因为js是单线程的,这实际上将冻结所有内容?

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. 如果全部同步,则是的,它将冻结所有内容,并且Promise不会解析,因为正在检查Promise状态的同步脚本正在阻止。

any messy way to wait for a promise synchronously ? 是否有任何麻烦的方式来同步等待诺言?

No, there is no sync way to wait for a Promise to resolve. 不,没有同步方法可以等待Promise解决。

Promises to don't create multi-threading in the JavaScript context. 承诺不要在JavaScript上下文中创建多线程。 Promises allow to make cooperative multitasking easier, especially with the introduction of await / async , but the code is still single threaded. 承诺使协作式多任务处理变得更容易,尤其是在引入await / async ,但是代码仍然是单线程的。

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时,请清除该时间间隔并调用任何函数以继续执行预期的执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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