Christopher Allen Christopher Allen - 1 month ago 5x
Node.js Question

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 as node.js from my research has no way to stop as required.... Its getting hard to read users information off after a period of time... iv seen some code out there, but i believe they have to have other codes inside of them for them to work such as:

setTimeout(function() {
}, 3000);

But this issue is, i need everything after this line of code to execute after the period of time...

For example,

console.log('Welcome to My Console,');
console.log('Blah blah blah blah extra-blah');

I've also seen things like

yield sleep(2000);

But node.js doesnt recognize this....

If anybody is willing to help, it is so much appreciated.


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

// 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.