setTimeout的Javascript返回值 [英] Javascript return value from setTimeout

查看:112
本文介绍了setTimeout的Javascript返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做这样的事情:

var x = function(){
  if (controlVar === 0) {
    setTimeout(x, 300);
  }
  else {
    return value;
};

js中是否有一种方法可以像同步代码中那样调用函数(var z = x())并仅在我的controlVar上交1时才返回值?

Is there a method in js for call a function like in synchronous code (var z = x()) and return a value only when my controlVar turn in 1?

这不应该阻塞,因为当我使用setTimeout时,主循环应该可以控制,但是如果阻塞对我来说无关紧要.

This should not block, because when I use setTimeout the main loop shoul be able to take the control, but if block doesn't matter for me.

谢谢.

推荐答案

js中是否有一种方法可以像同步代码中那样调用函数( var z = x())并仅在我的controlVar上交1时才返回值?

Is there a method in js for call a function like in synchronous code (var z = x()) and return a value only when my controlVar turn in 1?

不.分配是同步的.它不能等待异步函数.更准确地说, x 立即返回,它不等待超时.

No. Assignment is synchronous. It can't wait for an asynchronous function. To be more precise, x immediately returns, it doesn't wait for the timeout.

setTimeout 只是将一个新作业添加到作业队列中.仅在当前作业完成后,才会处理下一个作业.

setTimeout simply adds a new job to the job queue. Only after the current job is finished, the next job will be processed.

我想调用一个递归函数,该函数仅在充当信号量的变量变为绿色时才返回.

I want call a recursive function that return only when a variable that act like a semaphore turn green.

这在JavaScript中不起作用,因为它具有完成" :

That doesn't work in JavaScript because it has the concept of "run to completion":

在处理任何其他消息之前,已完全处理每个消息.在推理程序时,这提供了一些不错的属性,包括以下事实:每当函数运行时,它都不会被抢占,并且会在其他任何代码运行之前完全运行(并且可以修改函数操作的数据).例如,这与C不同,在C中,如果函数在线程中运行,则可以在任何时候将其停止以在另一个线程中运行其他代码.

Each message is processed completely before any other message is processed. This offers some nice properties when reasoning about your program, including the fact that whenever a function runs, it cannot be pre-empted and will run entirely before any other code runs (and can modify data the function manipulates). This differs from C, for instance, where if a function runs in a thread, it can be stopped at any point to run some other code in another thread.

回到您的示例,我们首先评估脚本并调用 x .这是作业队列的当前状态:

Going back to your example, we start with evaluating the script and calling x. This is the current status the job queue:

Job Queue--------------------------------------------------+
| +---------------------+                                  |
| |What: evaluate script|                                  |
| |                     |                                  |
| |                     |                                  |
| +---------------------+                                  |
+----------------------------------------------------------+

当执行 setTimeout(x,300); 行时,新条目将添加到作业队列:

When the line setTimeout(x, 300); is executed, a new entry is added to the job queue:

Job Queue--------------------------------------------------+
| +---------------------+    +-----------------+           |
| |What: evaluate script|    |What: execute x  |           |
| |                     |    |When: 300ms      |           |
| |                     |    |                 |           |
| +---------------------+    +-----------------+           |
+----------------------------------------------------------+

为了处理下一个条目,必须先完成当前作业.这意味着,由于我们当前正在调用 x ,因此 x 必须终止.只有在返回 x 后,事件循环才能继续进行下一个作业,然后再次调用 x .

In order for the the next entry to be processed, the current job must be completed first. That means, since we are currently calling x, x must terminate. Only after x returned, the event loop can move on to the next job and call x again.

希望这使 x 无法等待超时的原因更加清楚.

Hopefully this makes it a bit clearer why x cannot wait for the timeout.

有关其他解决方案,请参见如何退回来自异步调用的响应?

For alternative solutions see How do I return the response from an asynchronous call? .

这篇关于setTimeout的Javascript返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆