Jump to content

Javascript clock not working


Recommended Posts

Hello, I'm creating a Javascript military clock widget and everything appears to be correct as far as I can tell, but it's not working. Any ideas? Here's my code: window.onload = function time_func(){var ct = new Date();var h = ct.getHours();var m = ct.getMinutes(); setTimeout(time_func(), 1000);} var $time = {military: h + ":" + m} [then in my HTML file]<script type="text/javascript">document.write($time.military);</script>

Link to post
Share on other sites

There are multiple errors. This syntax is incorrect:window.onload = function time_func() {It's eitherwindow.onload = function() {orwindow.onload = time_func; In order to pass a reference to setTimeout you must omit parentheses. The parentheses tell the function to execute immediately.This is the correct way to pass the function to setTimeout:

setTimeout(time_func, 1000);

This object is using "h" and "m" which are not in the global scope. This means that they don't really exist:

var $time = {military: h + ":" + m}

What you really need to do is update this object inside the function and use it inside the function. This code will execute once and then won't execute anymore:

document.write($time.military);

What you need to do is to make the function update the contents of an HTML node every time the value changes.

Link to post
Share on other sites

Ah, okay. Thanks! Do you think there's any possible way to be able to make $time.military work? Like $time = {military: "<span id='clock'></span>"}

Link to post
Share on other sites

Inside the function you can give it value you like.

var ct = new Date();var h = ct.getHours();var m = ct.getMinutes();$time.military = h + ":" + m;

However, you'll need to put that value into an HTML element.The easiest way to reference an element is by its ID.

var clockElement = document.getElementById("clock");

Link to post
Share on other sites

Hey Fox, Why is this syntax incorrect: window.onload = function time_func() {I have seen a lot of JavaScript that have something like:window.onload = function init() { alert("Hello"); // just an eample }

Link to post
Share on other sites
Hey Fox, Why is this syntax incorrect: window.onload = function time_func() { I have seen a lot of JavaScript that have something like: window.onload = function init() { alert("Hello"); // just an eample }
What you've seen is this:
window.onload = function() {    // Code}

Link to post
Share on other sites

Dont know if that be usefull but maybe there working code will help ya

$head.='	   <td class="clock">'."\n";$head.='	    <span id="date">'.date('Y/m/d').'</span><br />'."\$head.='	    <span id="time">'.date('H:i:s').'</span>'."\n";$head.='	    <span id="stamp">'.(time()*1000).'</span>'."\n";$head.='	   </td>'."\n";

function clock(stampId,dateId,timeId){var now=new Date(parseInt($('#'+stampId).html()));now.setSeconds(now.getSeconds()+1);$('#'+dateId).html(now.getFullYear()+'/'+(now.getMonth()<9?'0'+(now.getMonth()+1):(now.getMonth()+1))+'/'+(now.getDate()<10?'0'+now.getDate():now.getDate()));$('#'+timeId).html((now.getHours()<10?'0'+now.getHours():now.getHours())+':'+(now.getMinutes()<10?'0'+now.getMinutes():now.getMinutes())+':'+(now.getSeconds()<10?'0'+now.getSeconds():now.getSeconds()));$('#'+stampId).html(now.getTime());setTimeout('clock(\''+stampId+'\',\''+dateId+'\',\''+timeId+'\');',1000);}$(document).ready(function(){clock('stamp','date','time');});

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...