我如何在Node.js(Javascript)中等待,我需要暂停一段时间

本文翻译自:How Can I Wait In Node.js (Javascript), l need to pause for a period of time

I'm developing a console like script for personal needs. 我正在为个人需求开发类似脚本的控制台。 I need to be able to pause for a extended amount of time, but, from my research, node.js has no way to stop as required. 我需要能够暂停很长一段时间,但是,根据我的研究,node.js无法按需停止。 It's getting hard to read users' information after a period of time... I've seen some code out there, but I believe they have to have other code inside of them for them to work such as: 一段时间后,读取用户的信息变得越来越困难...我已经看到了一些代码,但是我相信他们必须在其中包含其他代码才能使他们工作,例如:

setTimeout(function() {
}, 3000);

However, I need everything after this line of code to execute after the period of time. 但是,我需要这段代码之后的所有内容才能在一段时间后执行。

For example, 例如,

//start-of-code
console.log('Welcome to My Console,');
some-wait-code-here-for-ten-seconds..........
console.log('Blah blah blah blah extra-blah');
//endcode. 

I've also seen things like 我也看过类似的东西

yield sleep(2000);

But node.js doesnt recognize this. 但是node.js无法识别这一点。

How can I achieve this extended pause? 我如何才能实现这种长时间的暂停?


#1楼

参考:https://stackoom.com/question/xmwk/我如何在Node-js-Javascript-中等待-我需要暂停一段时间


#2楼

Put the code that you want executed after the delay within the setTimeout callback: 将要在延迟后执行的代码放在setTimeout回调中:

console.log('Welcome to My Console,');
setTimeout(function() {
    console.log('Blah blah blah blah extra-blah');
}, 3000);

#3楼

Best way to do this is to break your code into multiple functions, like this: 最好的方法是将代码分成多个功能,如下所示:

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);

It's similar to JohnnyHK 's solution, but much neater and easier to extend. 它与JohnnyHK的解决方案相似,但是更加整洁并且易于扩展。


#4楼

Since, javascript engine (v8) runs code based on sequence of events in event-queue, There is no strict that javascript exactly trigger the execution at after specified time. 由于javascript引擎(v8)根据事件队列中的事件序列运行代码,因此没有严格要求javascript在指定时间后准确触发执行。 That is, when you set some seconds to execute the code later, triggering code is purely base on sequence in event queue. 也就是说,当您设置几秒钟以稍后执行代码时,触发代码将完全基于事件队列中的顺序。 So triggering execution of code may take more than specified time. 因此,触发代码执行可能要花费超过指定的时间。

So Node.js follows, 因此,Node.js紧随其后,

process.nextTick()

to run the code later instead setTimeout(). 以便稍后运行代码,而不是setTimeout()。 For example, 例如,

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

#5楼

This question is quite old, but recently V8 has added Generators which can accomplish what the OP requested. 这个问题已经很老了,但是最近V8添加了可以完成OP要求的生成器。 Generators are generally easiest to use for async interactions with the assistance of a library such as suspend or gen-run . 在诸如suspendgen-run之类的库的帮助下,生成器通常最容易用于异步交互。

Here's an example using suspend: 这是一个使用suspend的例子:

suspend(function* () {
    console.log('Welcome to My Console,');
    yield setTimeout(suspend.resume(), 10000); // 10 seconds pass..
    console.log('Blah blah blah blah extra-blah');
})();

Related reading (by way of shameless self promotion): What's the Big Deal with Generators? 相关阅读(通过无耻的自我提升): 发电机有什么大不了的? .


#6楼

I've recently created simpler abstraction called wait.for to call async functions in sync mode (based on node-fibers). 我最近创建了一个名为wait.for的更简单的抽象,以在同步模式下(基于节点光纤)调用异步函数。 There is also a version based on upcoming ES6 Generators. 还有一个基于即将推出的ES6 Generators的版本。

https://github.com/luciotato/waitfor https://github.com/luciotato/waitfor

Using wait.for , you can call any standard nodejs async function, as if it were a sync function, without blocking node's event loop. 使用wait.for ,您可以调用任何标准的nodejs异步函数,就好像它是同步函数一样,而不会阻塞节点的事件循环。

You can code sequentially when you need it, which is, (I'm guessing) perfect to simplify your scripts for personal use. 您可以在需要时按顺序进行编码,这(非常适合)可以简化您的脚本供个人使用。

using wait.for your code will be: 使用wait.for您的代码将是:

require('waitfor')

..in a fiber..
//start-of-code
console.log('Welcome to My Console,');
wait.miliseconds(10*1000); //defined in waitfor/paralell-tests.js - DOES NOT BLOCK
console.log('Blah blah blah blah extra-blah');
//endcode. 

Also any async function can be called in Sync mode. 同样,任何异步功能都可以在同步模式下调用。 Check the examples. 检查示例。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值