Tuesday, September 11, 2018

JavaScript - Built in Functions - setTimeout and setInterval

There are two very import built-in functions in JavaScript. This registered in window object.

  1. setTimeout
  2. setInterval


setTimeout: This function accepts two arguments.

  1.  Function , named or closures (anonymous functions)
  2.  Time period in milliseconds, this setTimeout must wait before executing the function.


Example:

setTimeout(function(){console.log("Here I am");},2000);

setInterval: Another important function is the setInterval function which can be used to call periodically a function.

We can use setInterval in combination with setTimeout to create really interesting stuffs. Below is one example.



var interval = setInterval(function(){console.log("Inside setInterval");},200);

setTimeout(function(){clearInterval(interval);},1000)





Refer:
https://alligator.io/js/settimeout-setinterval/

Practice at:
http://jsbin.com

No comments:

Post a Comment