setTimeout is used in JQuery to delay execution of some code
In this souce code:
– Basic syntax
– Call on button click basic
– Call on button click advanced (with setTimeout(), you can also make a call to another function)
The Source Code:
<!DOCTYPE html> <html> <!-- Don't Break My b***s - Gimme Code! Project --> <!-- Author: Andrea Tonin - http://blog.lucedigitale.com --> <!-- This code come with absolutely no warranty --> <!-- If you use this code in your project please give a link to http://blog.lucedigitale.com --> <!-- Thanks in advance --> <head> <script type="text/javascript" src="js/jquery-2.0.3.js"></script> <script type="text/javascript"> // setTimeout is used in JQuery to delay execution of some code $(document).ready(function(){ // BASIC SYNTAX START setTimeout(function() { // Do something after 2 seconds alert('2 seconds'); }, 2000); // The function takes the times in miliseconds. // BASIC SYNTAX END // CALL ON BUTTON CLICK BASIC START $( "#btn1" ).click(function() { setTimeout(function() { // Do something after 2 seconds alert('2 seconds after button click - BASIC'); }, 2000); // The function takes the times in miliseconds. }); // CALL ON BUTTON CLICK BASIC END // CALL ON BUTTON CLICK ADVANCED START // with setTimeout(), you can also make a call to another function $(document).ready(function() { $('#btn2').bind('click', function() { Function1(); }); function Function1() { setTimeout(function () { Function2(); }, 2000); } function Function2() { alert('2 seconds after button click - ADVANCED'); } }); // CALL ON BUTTON CLICK ADVANCED END }); </script> </head> <body> <p>After 2 seconds you will see an alert window...</p> <p><button id="btn1">After 2 seconds open an alert window - basic syntax</button></p> <p><button id="btn2">After 2 seconds open an alert window - advanced syntax</button></p> </body> </html>DEMO