Pages

Sunday, May 28, 2017

nodejs settimeout arguments

nodejs settimeout  arguments

settimeout is a javascript method used to invoke a method in a particular latency.
When we say methods, a method may or may not have arguments / parameters. This post shows the syntax of invoking a javascript function with / without arguments via setTimeout method.

With Arguments / Parameters

Suppose we have method called sayHello with the argument name,
function sayHello( name ) {
  console.log("Hey " + name );
}

To invoke the method via setTimeout, the syntax will be,

setTimeout(sayHello.bind(null, "User"), 4000);
 
where ,4000 -> 4 seconds in ms
       User -> argument to the method sayHello 
 
In General, 
 
Function.prototype.bind()
 
http://stackoverflow.com/questions/1190642/how-can-i-pass-a-parameter-to-a-settimeout-callback  
 

No comments:

Post a Comment