JavaScript : Clock Example

To following this step, you should know about "setTimeout()" methods and JavaScript DateTime objects. "setTimeout()" is look like recursive function (recursion) that allowed add delay before it action. "setTimeout()" is required Method's name that you want to repeat and delay time (millisecond).

Syntax
   setTimeout("MethodName()","DelayTime")


Example Clock
This example used to create interactive clock on your webpage by using setTimeout() and show output on <div id="divOutput">. You can move this tag everywhere to show your clock on your webpage.


<script language="JavaScript">
   function callTime() {
      var d = new Date();
      var result = d.getDate()+"/";
      result += (d.getMonth()+1)+"/";
      result += d.getYear() + " ";
      result += d.getHours() + ":"
      result += d.getMinutes() + ":";
      result += d.getSeconds();


      document.getElementById("divOutput").innerHTML = result;
      setTimeout("callTime()", 1000);
}
</script>


<body onload="callTime();">
<div id="divOutput"> </div>
</body>
##############################


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...