简体   繁体   English

通过延迟函数javascript阻止执行

[英]block execution by delay function javascript

I am using the following code to see if the JavaScript execution gets blocked while a synchronous function gets into a long loop.我正在使用以下代码查看 JavaScript 执行是否在同步函数进入长循环时被阻止。

function delayBySeconds(sec){
  let start = now = Date.now();
  while((now - start) < (sec * 1000)){
    //console.log(now);
    now = Date.now();
  }
}

const seconds = 20;
delayBySeconds(seconds);
console.log(`${seconds} seconds passed`);

It prints the message immediately which shows the execution doesn't wait for the called function to complete its loop.它立即打印消息,显示执行不等待被调用函数完成其循环。 How does that work?这是如何运作的? I mean if the delayBySeconds function works properly, how does the message get printed immediately?我的意思是如果 delayBySeconds 函数正常工作,消息如何立即打印? It's only experimental, of course, it doesn't make sense I would use it for some practical delay.这只是实验性的,当然,我将它用于一些实际的延迟是没有意义的。

It's a synchronous code execution.这是一个同步代码执行。 The console is waiting until the function is not executed fully.控制台正在等待,直到函数没有完全执行。 You are not seeing any delay because you are not performing any task inside the loop.您没有看到任何延迟,因为您没有在循环内执行任何任务。

You can see the delay if you uncomment the console inside the while loop.如果在 while 循环中取消对控制台的注释,您可以看到延迟。

Because of the fact that JavaScript is always synchronous and single-threaded, it does not wait for your function execution to finish before moving on to next statement.由于 JavaScript 始终是同步和单线程的,因此它不会等待您的函数执行完成,然后再转到下一条语句。 Therefore, to properly wait for a function to finish execution, we used Promise/Then sort-of "framework" to make asynchronous call to function, ie waiting for it to complete before moving on.因此,为了正确等待函数完成执行,我们使用了 Promise/Then 类型的“框架”来异步调用函数,即在继续之前等待它完成。 Now after ES2017, we can use async/await to kinda simplify the process.现在在 ES2017 之后,我们可以使用 async/await 来简化这个过程。

Snippet using Promise使用 Promise 的片段

const delayBySeconds = sec => new Promise(resolve => {
  let start = now = Date.now();
  while((now - start) < (sec * 1000)){
    // console.log(now - start);
    now = Date.now();
  }
  resolve()
})

const seconds = 10;
delayBySeconds(seconds).then(()=>
console.log(`${seconds} seconds passed`))

snippet using async/await使用 async/await 的片段

const delayBySeconds = async sec => {
  let start = now = Date.now();
  while((now - start) < (sec * 1000)){
    // console.log(now - start);
    now = Date.now();
  }
  return
}

const main = async () => {
  const seconds = 7;
  await delayBySeconds(seconds)
  console.log(`${seconds} seconds passed`)
}

main()

Notice that async/await can only be used in an async function, so you have to wrap it up in a function if you are using native js.注意 async/await 只能在 async 函数中使用,所以如果你使用原生 js,你必须把它封装在一个函数中。 However, some, like Deno, a fairly recent backend js framework, has global support for async/await so you don't have to wrap it up in there.然而,有些像 Deno,一个相当新的后端 js 框架,具有对 async/await 的全球支持,所以你不必把它包装在那里。

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

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