JS Tip - Use variable parameters for setTimeout()

setTimeout parameters


setTimeout function allows us to sets a timer which executes a function or specified piece of code once the timer expires. Generally to call a function with parameter inside setTimeout we wrap the function inside anonymous function.

But we can pass the function parameters directly !!


 const logger = message => {  
   console.log(message);  
 };  
   
 // Using anonymous function  
 setTimeout(() => {  
   logger("Hello!!");  
 }, 1000);  
   
 // Pass parameter to callback function directly  
 // setTimout(function, delay, ..args)
 setTimeout(logger, 1000, 'How are you?')  

Comments

Popular posts from this blog

JS Tip - Required function parameters

Disallow specific imports in Javascript Project