Skip to content
Fix Code Error

How can I wait In Node.js (JavaScript)? l need to pause for a period of time

March 13, 2021 by Code Error
Posted By: Anonymous

I’m developing a console script for personal needs. I need to be able to pause for an extended amount of time, but, from my research, Node.js has no way to stop as required. 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');
    // end of code

I’ve also seen things like

    yield sleep(2000);

But Node.js doesn’t recognize this.

How can I achieve this extended pause?

Solution

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.

Answered By: Anonymous

Related Articles

  • Problems Installing CRA & NextJS from NPM…
  • What are the undocumented features and limitations…
  • Taking an object's element and turning it into a…
  • Ukkonen's suffix tree algorithm in plain English
  • What's the difference between eval, exec, and compile?
  • How to set HTML5 required attribute in Javascript?
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • How to filter a RecyclerView with a SearchView
  • For-each over an array in JavaScript
  • Gradle error: Execution failed for task…
  • What is a NullReferenceException, and how do I fix it?
  • What does this symbol mean in JavaScript?
  • What is your most productive shortcut with Vim?
  • data.table vs dplyr: can one do something well the…
  • Reference - What does this regex mean?
  • org.gradle.api.tasks.TaskExecutionException:…
  • How to paste yanked text into the Vim command line
  • What does "Fatal error: Unexpectedly found nil while…
  • How does PHP 'foreach' actually work?
  • What are the new features in C++17?
  • How to make the script wait/sleep in a simple way in unity
  • How to update other fields based on a field value in…
  • Transferring JavaScript objects to HTML table
  • Usage of __slots__?
  • What is the scope of variables in JavaScript?
  • How to use Servlets and Ajax?
  • How to generate a random string of a fixed length in Go?
  • Understanding PrimeFaces process/update and JSF…
  • How do I return the response from an asynchronous call?
  • commandButton/commandLink/ajax action/listener…
  • Examples of GoF Design Patterns in Java's core libraries
  • Error: container has not been made global - how to solve?
  • Rails wrong number of arguments error when…
  • Identifying and solving…
  • How to dispatch a Redux action with a timeout?
  • Jetpack Compose and Hilt Conflict
  • VUE Error when run test unit
  • Calculate sum for group of dynamic table rows in jquery
  • Execution failed for task…
  • Why does C++ code for testing the Collatz conjecture…
  • Vue&TypeScript: how to avoid error TS2345 when…
  • How does the "this" keyword work?
  • How do Mockito matchers work?
  • What is an optional value in Swift?
  • SQL query return data from multiple tables
  • How do I approach solving this problem: Cannot…
  • Callback functions in C++
  • Reduce array according to grouping condition in reactjs
  • Difference between variable declaration syntaxes in…
  • What does a "Cannot find symbol" or "Cannot resolve…
  • Why doesn't the height of a container element…
  • Extending native HTML Elements with Polymer
  • Why do I have to "git push --set-upstream origin "?
  • No 'Access-Control-Allow-Origin' header is present…
  • The definitive guide to form-based website authentication
  • Why do git fetch origin and git fetch : behave differently?
  • In plain English, what does "git reset" do?
  • How to detect if multiple keys are pressed at once…
  • When I'm testing a web app by JUnit and Mockito I…
  • In CSS Flexbox, why are there no "justify-items" and…
  • How to use java.net.URLConnection to fire and handle…
  • How can I view the source code for a function?
  • How can I use/create dynamic template to compile…
  • How can I access and process nested objects, arrays or JSON?
  • How to "properly" create a custom object in JavaScript?
  • ExpressJS How to structure an application?
  • How to recover stashed uncommitted changes
  • What is an IndexOutOfRangeException /…
  • javax.faces.application.ViewExpiredException: View…
  • Best practice multi language website
  • How can I exclude all "permission denied" messages…
  • Express Session not persisting after CORS calls
  • How do I include a JavaScript file in another…
  • jQuery Mobile: document ready vs. page events
  • Logging best practices
  • How do I count unique visitors to my site?
  • Error: request entity too large
  • Replacing a 32-bit loop counter with 64-bit…
  • PHP parse/syntax errors; and how to solve them
  • How can I find the product GUID of an installed MSI setup?
  • Laravel blade template changing to vue component
  • How to find out client ID of component for ajax…
  • Why call git branch --unset-upstream to fixup?
  • What is Ember RunLoop and how does it work?
  • How to specify typecllass function type to operate…
  • What are type hints in Python 3.5?
  • Flex: REJECT rejects one character at a time?
  • NPM vs. Bower vs. Browserify vs. Gulp vs. Grunt vs. Webpack
  • Changing PowerShell's default output encoding to UTF-8
  • Java string to date conversion
  • Setting up and using Meld as your git difftool and mergetool
  • Why is my variable unaltered after I modify it…
  • How do you run a command for each line of a file?
  • Checkout another branch when there are uncommitted…
  • Iterator invalidation rules
  • Does the code after the recursion call get executed…
  • How to create a temporary table in SSIS control flow…
  • What's the difference between Instant and LocalDateTime?
  • Where and why do I have to put the "template" and…
  • Express node worked as pushState enabled server…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Run R script from command line

Next Post:

Encoding as Base64 in Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error