JavaScript睡眠 [英] JavaScript sleep

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

问题描述

是的,我知道 - 这个问题有成千上万的答案。请不要告诉我关于 setTimeout 的方法,因为 - 是的,一切皆有可能,但不是那么容易,比如使用 sleep()方法。

Yes, I know - that question has thousands of answers. please, don't tell me about setTimeout method because - yes, everything is possible with that but not so easy as using sleep() method.

例如:

function fibonacci(n) {
    console.log("Computing Fibonacci for " + n + "...");
    var result = 0;

    //wait 1 second before computing for lower n
    sleep(1000);
    result = (n <= 1) ? 1 : (fibonacci(n - 1) + fibonacci(n - 2));

    //wait 1 second before announcing the result
    sleep(1000);
    console.log("F(" + n + ") = " + result);

    return result;
}

如果您知道如何使用获得相同的结果setTimeout - 告诉我;)fibanacci是非常简单的任务,因为有不超过2次递归,但是如何进行n次递归(如fib(1)+ fib(2)+ .. + fib(n))并在每个+之后睡觉?不,睡觉会轻松多了。

if you know how to get the same result using setTimeout - tell me ;) fibanacci is pretty easy task, because there aren't more than 2 recursions, but how about n-recursions (like fib(1) + fib(2) + .. + fib(n)) and sleep after every "+"? Nah, sleep would be muuuuuch easier.

但我仍然无法实现它的实例。 while(curr - start< time){curr =(...)} 很棘手,但它不起作用(只是停止浏览器然后抛出所有控制台)一次登录)。

But still I can't get working example of implementing it. while (curr - start < time) { curr = (...) } is tricky, but it won't work (just stops my browser and then throw all console logs at once).

推荐答案

我不完全明白你在问什么,但我要回答这一部分:

I dont fully understand what you're asking, but I'm going to answer this part:


如果你知道如何使用setTimeout得到相同的结果
- 告诉我

if you know how to get the same result using setTimeout - tell me

根本区别在于 sleep (在许多其他语言中使用)是同步的,而 setTimeout (以及许多其他JavaScript概念,例如AJAX)是异步的。因此,为了重写您的功能,我们必须考虑到这一点。主要是,我们必须使用回调来获取返回值,而不是实际的返回语句,因此它将像这样使用:

The fundamental difference is that sleep (as used in many other languages) is synchronous, while setTimeout (and many other JavaScript-concepts, like AJAX for example) are asynchronous. So, in order to rewrite your function we have to take this into account. Mainly, we have to use a callback to fetch the "return value", rather than an actual return-statement, so it will be used like this:

fibonacci(7, function(result) {
  // use the result here..
});

因此,至于实施:

function fibonacci(n, callback) {
  console.log("Computing Fibonacci for " + n + "...");
  var result = 0;

  var announceAndReturn = function() {
    setTimeout(function() {
      // wait 1 second before announcing the result
      console.log("F(" + n + ") = " + result);
      callback(result); // "returns" the value
    }, 1000);
  };

  // wait 1 second before computing lower n
  setTimeout(function() {
    if (n <= 1) {
      result = 1;
      announceAndReturn();
    }
    else {
      var resultsLeft = 2;

      var handler = function(returned) {
        result += returned;
        resultsLeft--;
        if (resultLeft == 0)
          announceAndReturn();
      }

      fibonacci(n-1, handler);
      fibonacci(n-2, handler);
    }
  }, 1000);
}

我还想指出,没有,这不是比使用 sleep 更容易的解决方案。为什么?因为这段代码是异步的,而且比大多数人习惯的同步代码简单得多。以这种方式开始思考需要练习。

I would also like to point out that, no, this is not an easier solution than using sleep. Why? Because this code is asynchronous and that's simply more complicated than synchronous code for what most people are used to. It takes practice to start thinking in that way.

好处?它允许您编写优于同步对应的非阻塞算法。如果您之前没有听说过 Node.js ,可以查看它以进一步了解这一点的好处。 (许多其他语言也有用于处理异步IO的库,但只要谈论JavaScript ..)

The upside? It allows you to write non-blocking algorithms that outperforms their synchronous counterparts. If you haven't heard of Node.js before, you could check it out to further understand the benefits of this. (Many other languages have libraries for dealing with async IO as well, but as long as were talking about JavaScript..)

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

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