In
my previous posts, I explained how node.js works asynchronously using even
listeners, call backs. There is another way to work asynchronously using timing
functions.
a. setTimeout(callback,
delay[, ...args])
b. setInterval(callback,
delay[, ...args])
setTimeout(callback,
delay[, ...args])
Callback
function is called when the timer elapses.
Below
table summarizes the arguments of setTimeout method.
Argument
|
Description
|
Callback
|
Function
to call when the timer elapses
|
Delay
|
Number
of milliseconds to wait before calling callback function
|
args
|
Optional
arguments to pass when the callback is called.
|
Let
me explain with an example.
var timeout = 3000; //3 seconds setTimeout(function(){ console.log("Hello, I woke up after 3 seconds"); }, timeout); console.log("Reached End");
Output
Reached
End
Hello,
I woke up after 3 seconds
In
the above example, callback function is called after 3 seconds.
Points to note
a.
When delay is larger than 2147483647 or less than 1, the delay will be set to
1.
b.
If callback is not a function, a TypeError will be thrown.
Passing parameters to
setTimeout function
By
using Optional arguments, you can pass arguments to the callback when it is
called.
HelloWorld.js
var timeout = 3000; // 3 seconds /* Function reference to a callback function */ var funRef = function(arg1, arg2) { console.log("Hello, I woke up after 3 seconds"); console.log("Arguments passed : %s, %s", arg1, arg2); }; /* funRef is called after 3 seconds and pass the optional arguments to funRef */ setTimeout(funRef, timeout, "Demo of setTimeout by", "Krishna"); console.log("Reached End");
Output
Reached
End
Hello,
I woke up after 3 seconds
Arguments
passed : Demo of setTimeout by, Krishna
Clear the timeout
‘clearTimeout(timeout)’
method cancels a Timeout object created by setTimeout().
var timeout = 3000; // 3 seconds var timeout = setTimeout(function(){ console.log("I woke up after 3 seconds..."); }, timeout); console.log("Clearing the time out"); clearTimeout(timeout);
Output
Clearing
the time out
setInterval(callback,
delay[, ...args])
setInterval
function is used to repeat the callback function for every delay milliseconds.
Below
table summarizes the arguments of setInterval function.
Argument
|
Description
|
callback
|
Function
to call when the timer elapses.
|
delay
|
Number
of milliseconds to wait before calling callback
|
args
|
Optional
arguments to pass, when the callback is called.
|
Let
me explain with an example.
var interval_time_out = 500; setInterval(function(){ console.log("Hi, I am here"); }, interval_time_out);
Output
Hi, I am here Hi, I am here Hi, I am here Hi, I am here Hi, I am here Hi, I am here Hi, I am here Hi, I am here …… …… ……
Message
‘Hi, I am here’ is printed for every 500 milliseconds.
Pass arguments to
callback function
By
using Optional arguments, you can pass arguments to the callback when it is
called.
var interval_time_out = 500; /* Function reference to a callback function */ var funRef = function(arg1, arg2) { console.log("Hello, I woke up every 500 milli seconds"); console.log("Arguments passed : %s, %s", arg1, arg2); }; setInterval(funRef, interval_time_out, "Demo of setInterval by", "Krishna");
Output
Hello, I woke up every 500 milli seconds Arguments passed : Demo of setInterval by, Krishna Hello, I woke up every 500 milli seconds Arguments passed : Demo of setInterval by, Krishna Hello, I woke up every 500 milli seconds Arguments passed : Demo of setInterval by, Krishna Hello, I woke up every 500 milli seconds Arguments passed : Demo of setInterval by, Krishna Hello, I woke up every 500 milli seconds Arguments passed : Demo of setInterval by, Krishna Hello, I woke up every 500 milli seconds Arguments passed : Demo of setInterval by, Krishna ........... ........... ...........
Points to Note:
a.
When delay is larger than 2147483647 or less than 1, the delay will be set to
1.
b.
If callback is not a function, a TypeError will be thrown.
clearInterval(timeout):
Cancel the setInterval
clearInterval(timeout)
method is used to cancels a Timeout object created by setInterval().
HelloWorld.js
var interval_time_out = 500; var interval_cancellation_time_out = 2000; var intervalRef = setInterval(function(){ console.log("Hi, I am here"); }, interval_time_out); setTimeout(function(){ clearInterval(intervalRef); console.log("Cancelled the interval timer"); }, interval_cancellation_time_out);
Output
Hi, I am here Hi, I am here Hi, I am here Cancelled the interval timer
No comments:
Post a Comment