0

I need to use <meta http-equiv="REFRESH"> to display the current time as it changes. This is what i tried:

<!DOCTYPE HTML>
<html>
    <head>
    <meta http-equiv="refresh" content="1">
    <script type="text/javascript">
    function change_time()
    {
    var d = new Date();
    var curr_hour = d.getHours();
    var curr_min = d.getMinutes();
    var curr_sec = d.getSeconds();  
    if(curr_hour > 12)
    curr_hour = curr_hour - 12;
    document.getElementById('time').innerHTML = curr_hour+':'+curr_min+':'+curr_sec;
    }
    change_time();
    </script>
    </head>

    <body>
    <table>
    <tr>
    <td>Current time is :</td>
    <td id="time"></td>
    <tr>
    </table> 

    </body>
</html>

The page refreshes itself every second but it doesn't not show the functions value.

beckinho
  • 33
  • 11

2 Answers2

0

Use this

<body onload="change_time()">

Remove change_time(); line just before closing </script> tag.

Edit:

You have forgot to call your javascript method on load/refresh of page.

To do so - use onload() event of body tag in html.

Arjit
  • 3,290
  • 1
  • 17
  • 18
0

You forgot to call your Javascript function when you have loaded the page. You can achieve that by using the onload attribute on the body node:

<body onload="change_time()">

See also: How do I call a JavaScript function on page load?

Community
  • 1
  • 1
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97