Saturday, 18 January 2014

jQuery Set Some Time Delay for Function / Method Execution with SetTimeout Function

Here I will explain how to execute function in jQuery with some time delay using setTimeout function or jQuery set some time delay for function or method execution with setTimeout function.
                                                                                   
Description:
  
In previous articles I explained
jQuery get query string parameter value with spaces, jQuery get query string parameter values, jQuery create rounded corners for textbox, jQuery show time in webpageand many articles relating to JQuery. Now I will explain how to get query string parameter value with special characters in jQuery.


Execute function with some time delay

To execute function with some time delay we need to write the code like as shown below


$(function() {
setTimeout("ShowTime()", 1000);
});
If you observe above code ShowTime function will execute after one second delay for complete example check below code

Example:


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<title>jQuery display current time on webpage</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function() {
setTimeout("ShowTime()", 1000);
});
function ShowTime() {
var dt = new Date();
$("#lblTime").html(dt.toLocaleTimeString());
setTimeout("ShowTime()", 1000); // Here 1000(milliseconds) means one 1 Sec
}
</script>
</head>
<body >
<form id="form1">
<div>
jQuery Display time second by second.
<label id="lblTime" style=" font-weight:bold"></label>
</div>
</form>
</body>
</html>
Live Demo

For live demo check below time for every one second function will execute and update the time




jQuery Display time second by second. 

No comments:

Post a Comment