node.js: while loop callback not working as expected

前端 未结 4 679
走了就别回头了
走了就别回头了 2021-02-06 04:36

Knowing that while Node.js is working asynchronously, writing something like this:

function sleep() {
    var stop = new Date().getTime();
    while(new Date().g         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-06 04:58

    You are calling sleep right away, and the new sleep function blocks. It keeps iterating until the condition is met. You should use setTimeout() to avoid blocking:

    setTimeout(function () {
        console.log('done sleeping');
    }, 15000);
    

提交回复
热议问题