1

I need to create a block to show a LIVE digital clock?

I tried to use the http://drupal.org/project/clock and the http://drupal.org/project/jstimer modules but it doesn't use javascript to refresh the time, it shows the time when the page is load, to refresh the time I need to reload the page.

How can I do this?

Something like this http://drupal.org/project/timeblock but for D7

  • 1
    have you looked at how simple the timeblock module is and how easily it could be incorporated into a/your D7 site? – Jimajamma Dec 22 '11 at 17:47
  • 1
    (although its use of a body element is puzzling/troublesome :) – Jimajamma Dec 22 '11 at 17:54
  • Amazing what one can find when you start peeking in 3rd party code. – stefgosselin Dec 23 '11 at 8:11
2

100% agree with Jimajamma above on extracting the stuff you need from the timeblock module, all it is really doing is just adding two javascript blocks with a minimal amount of formatting. For example, to replicate the 24-hour clock, all you have to do is create a new block with an 'unformatted' input format and add this code (extracted from the time block module):

<span id="liveclock" style="position:relative;left:0;top:0;">
</span>

<script language="JavaScript">
<!--

function show5(){
 if (!document.layers&&!document.all&&!document.getElementById)
 return
 var Digital=new Date()
 var hours=Digital.getHours()
 var minutes=Digital.getMinutes()
 var seconds=Digital.getSeconds()
 if (hours<12)
 hours="0"+hours
 if (minutes<=9)
 minutes="0"+minutes
 if (seconds<=9)
 seconds="0"+seconds
myclock="<strong>"+hours+":"+minutes+":"+seconds+"</strong>"
if (document.layers){
document.layers.liveclock.document.write(myclock)
document.layers.liveclock.document.close()
}
else if (document.all)
liveclock.innerHTML=myclock
else if (document.getElementById)
document.getElementById("liveclock").innerHTML=myclock
setTimeout("show5()",1000)
 }

$(document).ready(function(){ show5(); });


//-->
</script>

Quick fix to replace the body unload with a jquery $(document).ready() call, though probably not necessary inside a block.

| improve this answer | |
  • 1
    yep, and fix the formatting bug still found in the original code: if (hours<12) should be if (hours<=9) so you have 01-09 but not 011 or 010 :) – Jimajamma Dec 22 '11 at 23:48
  • yeah. Nice catch ;) – stefgosselin Dec 23 '11 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.