Jump to content

Javascript clock not working


Jacedc

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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...