Hi Guest 1 HomePage 1 Jobs 1 WalkIn 1Articles  

 :) Latest Topic
Node.js Tutorial
Parts of Node.js
Features of Node.js
Install Node.js Windows
Install Node.js Linux
Node.js First Example
Node.js Console
Node.js JavaScript Learn
Node.js REPL
Node.js NPM
Node.js CL Options1
Node.js CL Option2
Node.js Globals
Node.js OS
Node.js Timer
Node.js Errors
Node.js DNS
Node.js Net
Node.js Crypto
Node.js TLS/SSL
Node.js Debugger
Node.js Process
Node.js Process Example
Node.js Process Example2
Node.js Child Process
Child Process EXEC
Child Process SPAWN
Child Process Fork
Node.js Buffers
Node.js Streams
Node.js Reading Stream
Node.js Writing Stream
Node.js Piping Stream
Node.js Chaining Streams
Node.js File System Part1
Node.js File System Part2
Node.js File System Part3
Node.js Path
Node.js StringDecoder
Node.js Query String
Node.js ZLIB
Node.js Assertion
Node.js V8
Node.js Callbacks
Node.js Events
Node.js Punycode
Node.js TTY
Node.js Web Module
Node.js Upload Files
Node.js Email Send
Node.js BuiltIn Modules
Node.js Assert Code
Node.js Assert Function
Node.js console.time()
Node.js console.table()
Node.js DNS Code
Node.js cipher.final()
Node.js URL.hash API
Node.js URL.host API
Node.js URL.origin API
Node.js MySQL
Folders in Node.js
Folders Node.js part3
Rename Folder Node.js
Remove Folder Node.js
 


  

 
Node.js Timer
Question Posted on 02 Feb 2024

Home >> Tutorial >> Node.js Tutorial >> Node.js Timer


Node.js Timer

Node.js Timer



Well when we say about the TImer functions in Node.js, These are global functions. And you don't need to use require() function in order to use timer functions. Below are the list of timer functions.


Set Timer Functions


  • setImmediate():-This function is used to execute setImmediate.

  • setInterval():-This function is used to define a time interval.

  • setTimeout():-This function is used to execute a one-time callback after delay milliseconds.


Clear Timer Functions


  • clearImmediate(immediateObject):-This is used to stop an immediateObject, as created by setImmediate.

  • clearInterval(intervalObject):-This function is used to stop an intervalObject, as created by setInterval.

  • clearTimeout(timeoutObject):-This function is used to prevents a timeoutObject, as created by setTimeout.



Node.js Timer setInterval Example



Below example will set a time interval of 1000 millisecond and specified comment will be displayed after every 1000 millisecond until you terminate.

File: timer1.js


setInterval(function() {
console.log("setInterval: Hey! 1 millisecond completed!..");
}, 1000);



Now you will open Node.js command prompt and run the below code:-


node timer1.js





Timer NodeJS

File: timer5.js


var i =0;
console.log(i);
setInterval(function(){
i++;
console.log(i);
}, 1000);




Now open the node.js command prompt and then run the below code:


node timer5.js






timerloopnodejs


Node.js Timer setTimeout() Example



File: timer1.js


setTimeout(function() {
console.log("setTimeout: Hey! 1000 millisecond completed!..");
}, 1000);




Now open Node.js command prompt and then run the below command:-


node timer1.js






Node.js timer

This example will shows time out after every 1000 milisecond without setting a time interval. And this example uses the recursion property of a function.

File: timer2.js


var recursive = function () {
console.log("Hey! 1000 millisecond completed!..");
setTimeout(recursive,1000);
}
recursive();




Now open Node.js command prompt you need to run the following code:-


node timer2.js






Node.js timer



Node.js setInterval(), setTimeout() and clearTimeout()



Now let see an example to use clearTimeout() function

File: timer3.js


function welcome () {
console.log("Welcome to CrackYourInterview!");
}
var id1 = setTimeout(welcome,1000);
var id2 = setInterval(welcome,1000);
clearTimeout(id1);
//clearInterval(id2);




Now open node.js command prompt and run the below command or code:-


node timer3.js






Node.js timer

As you can see that the above example is recursive in nature. It will terminate after one step if you use ClearInterval.



Node.js setInterval(), setTimeout() and clearInterval()



Now let see an example to use clearInterval() function

File: timer3.js


function welcome () {
console.log("Welcome to CrackYourInterview!");
}
var id1 = setTimeout(welcome,1000);
var id2 = setInterval(welcome,1000);
//clearTimeout(id1);
clearInterval(id2);




Now open node.js command prompt and run the below command or code:-


node timer3.js






Node.js timer





Other Important Questions
Parts of Node.js
Install Node.js Linux
Node.js First Example
Node.js Globals
Node.js DNS







 
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |