10/22/2023 0 Comments Node js thread sleep![]() ![]() ![]() As javascript is single threaded, using setTimeout allows the thread to run another events which are waiting in the queue and makes execution faster. ![]() The Node.js setTimeout method can be used at various places in javascript code where user need some operation to be performed after a delay of time. All can be achieved by using setTimeout method. This becomes the major advantage of using setTimeout method is Node.js so that code execution, delay in response, processing response after a time period, redirecting or reloading contents after a time is exceeded, etc.Using Node.js framework, code runs asynchronously, and so at times requirement arises that the code execution which is asynchronous should be kept on hold for a minimum time period mention in setTimeout method.Given below are the advantages mentioned: To start the application run the command node index.js in CMD.Īs our Node Application is listening on port 3000, once CMD says the application has started to listen on port 300, open chrome window or any other browser and launch local application on http with port number as 3000. SetTimeout(delayedFuntion, delayTimeInMS) Ĭonsole.log('Demo Applications has started to listen on port 3000!') VardelayTimeInMS = 10000 //Defining time in msĬonsole.log("Payment page exceeded the time limit, please refresh the screen") Res.end() //Writing content to html using res.write and so on) įunctiondelayedFuntion(param1, param2. setTimeout with parameter setTimeout(delayedFuntion, delayTimeInMS, param1, param2. Second parameter: It is the minimum time of delay required and should always be defined in milliseconds.Ģ.Function on the fly or separately defined function both works fine here. First parameter: It should always be the function which needs delay in execution.setTimeout without parameter setTimeout(delayedFuntion, delayTimeInMS) setTimeout is mainly used when a particular block of code should be executed after a delay of few milliseconds also this time defined is the minimum time of wait before executing the callback function defined in setTimeout method. setTimeout is a built-in Node.js API function which executes a given method only after a desired time period which should be defined in milliseconds only and it returns a timeout object which can be used further in the process. The following article provides an outline for Node.js setTimeout. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |