Share Blog

Sunday, June 01, 2014

JavaScript setTimeout Function Example

Introduction

Here I will explain how to use JavaScript setTimeout function with example.JavaScript setTimeout()function is used to execute functions at regular intervals of time. Suppose if we want to execute oneJavaScript function for every 10 seconds or for a specific time we can use JavaScript setTimeout function to achieve this functionalit. 

----------------For complete example check below code--------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JavaScript display current time on webpage</title>
<script type="text/javascript">
    function ShowCurrentTime() {
        var dt = new Date();
        document.getElementById("lblTime").innerHTML = dt.toLocaleTimeString();
        setTimeout("ShowCurrentTime()", 1000); // Here 1000(milliseconds) means one 1 Sec
    }
</script>
</head>
<body onload="ShowCurrentTime()">
<div>
JavaScript Display current time second by second:
<label id="lblTime" style=" font-weight:bold"></label>
</div>
</body>
</html>

No comments:

Post a Comment