Wednesday, April 15, 2015

How to use javascript settimeout to call functions in sequence


Using Javascript setTimeout to call javascript functions is easy. This simple tutorial explains the usage of javascript setTimeout function



setTimeout(function() {alert('something');}, 1000);


This will call alert function after 1 sec.
NOTE: This javascript function should be enclosed between script tags


Javascript setTimeout Function example


Lets say we create a function to do something, in our case it is just an alert

function myFunction(){

   alert("Function called");

}




Calling function on setTimeout


We can call the earlier created function using setTimeout 

setTimeout(myFunction, 1000);



I would generally wrap this line in another function and call it. And my whole script could be 
something like this 


starter();


function starter() {

    //DO SOMETHING

 setTimeout(myFunction, 1000);

}


function myFunction(){    alert("Function called"); }


Calling functions in sequence

You can repeat the above procedure and call as many functions you can 



starter();


function starter() {
    //DO SOMETHING
 setTimeout(myFunction, 1000);

}

function myFunction() {  
   alert("Function called"); 
   setTimeout(mySecondFunction, 1000);  

}

function mySecondFunction() {
     alert("Second Function called");
  
} 



Fiddle for the same available here : http://jsfiddle.net/mannemvamsi/tgahstsd/

Next :: How to create to a simple sign in form


No comments:

Post a Comment