我正在为个人需求开发一个控制台脚本。我需要能够暂停更长的时间,但是,根据我的研究,Node.js没有办法根据需要停止。一段时间后,读取用户信息变得越来越困难了……我已经看到了一些代码,但我相信他们必须有其他的代码在他们的工作,如:

    setTimeout(function() {
    }, 3000);

但是,我需要这行代码之后的所有内容在一段时间之后执行。

例如,

    // start of code
    console.log('Welcome to my console,');

    some-wait-code-here-for-ten-seconds...

    console.log('Blah blah blah blah extra-blah');
    // end of code

我还见过

    yield sleep(2000);

但是Node.js不能识别这个。

我怎样才能实现这种延长的暂停?


当前回答

尝试使用promise,它在NodeJS中很管用

一个衬套

await new Promise(resolve => setTimeout(resolve, 5000));

或者把它作为NodeJS中的一个函数来重用

const sleep = async (milliseconds) => {
    await new Promise(resolve => setTimeout(resolve, milliseconds));
}

像这样使用函数

await sleep(5000)

其他回答

由于javascript引擎(v8)是基于事件队列中的事件顺序运行代码的,因此没有严格的规定javascript在指定的时间后准确地触发执行。也就是说,当您设置几秒后执行代码时,触发代码完全基于事件队列中的顺序。因此,触发代码执行的时间可能会超过指定的时间。

node。js遵循,

process.nextTick()

来代替setTimeout()运行代码。例如,

process.nextTick(function(){
    console.log("This will be printed later");
});

最好的方法是把你的代码分解成多个函数,像这样:

function function1() {
    // stuff you want to happen right away
    console.log('Welcome to My Console,');
}

function function2() {
    // all the stuff you want to happen after that pause
    console.log('Blah blah blah blah extra-blah');
}

// call the first chunk of code right away
function1();

// call the rest of the code and have it execute after 3 seconds
setTimeout(function2, 3000);

它类似于JohnnyHK的解决方案,但更整洁,更容易扩展。

function doThen(conditional,then,timer) {
    var timer = timer || 1;
    var interval = setInterval(function(){
        if(conditional()) {
            clearInterval(interval);
            then();
        }
    }, timer);
}

使用示例:

var counter = 1;
doThen(
    function() {
        counter++;
        return counter == 1000;
    },
    function() {
        console.log("Counter hit 1000"); // 1000 repeats later
    }
)

简单,我们将等待5秒钟来等待一些事件发生(这将由done变量在代码中的其他地方设置为true表示),或者当超时到期时,我们将每100ms检查一次

    var timeout=5000; //will wait for 5 seconds or untildone
    var scope = this; //bind this to scope variable
    (function() {
        if (timeout<=0 || scope.done) //timeout expired or done
        {
            scope.callback();//some function to call after we are done
        }
        else
        {
            setTimeout(arguments.callee,100) //call itself again until done
            timeout -= 100;
        }
    })();

2021年1月更新:你甚至可以在Node REPL交互中使用——experimental-repl-await标志来实现

$ node --experimental-repl-await
> const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
> await delay(1000) /// waiting 1 second.

老问题的新答案。今天(2017年1月至2019年6月)要容易得多。您可以使用新的async/await语法。 例如:

async function init() {
  console.log(1);
  await sleep(1000);
  console.log(2);
}

function sleep(ms) {
  return new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
}

为了在不安装插件的情况下使用async/await,你必须使用node-v7或node-v8,并使用——harmony标志。

2019年6月更新:通过使用最新版本的NodeJS,您可以开箱即用。不需要提供命令行参数。即使谷歌Chrome今天也支持它。

2020年5月更新: 很快你就可以在async函数之外使用await语法了。在顶层,比如这个例子

await sleep(1000)
function sleep(ms) {
  return new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
}

提案已进入第三阶段。 今天你可以通过webpack 5 (alpha)使用它,

更多信息:

Nodejs中的和谐标志:https://nodejs.org/en/docs/es6/ 所有NodeJS版本下载:https://nodejs.org/en/download/releases/