How can you make a live clock in 00:00 format? I've included my code down below. Is there a way to do this better or with shorter code?
HTML
<body onload=display_ct();>
<span id='ct'></span>
Now we just need to add the javascript part. This refreshes the clock every second so its "Live". Also it uses functions like getMinutes
to fetch the current time.
Javascript
function display_c(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_ct()',refresh)
}
function display_ct() {
var x = new Date()
x1 = x.getHours( )+ ":" + x.getMinutes();
document.getElementById('ct').innerHTML = x1;
display_c();
}
The 1000 milliseconds can be swapped out with more or less. 1000 is equal to 1 second so that would mean 500ms would be .5 seconds.
I'll include my code in an executable here:
function display_c(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_ct()',refresh)
}
function display_ct() {
var x = new Date()
x1 = x.getHours( )+ ":" + x.getMinutes();
document.getElementById('ct').innerHTML = x1;
display_c();
}
<body onload=display_ct();>
<span id='ct'></span>