We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

iTony on 03/18/08


Tagged

javascript binary clock snarkles


Versions (?)


Binary clock


Published in: JavaScript 


URL: http://snarkles.net/scripts/snubt/

this guy made a binary clock in javascript, i still need to know how a binary clock works.

  1. /*******************************************************************************
  2. * Snarkles.Net Useless Binary Time (SNUBT)
  3. *
  4. * ThinkGeek's Binary LED Clock... in JavaScript!
  5. * http://www.thinkgeek.com/cubegoodies/lights/59e0/
  6. *
  7. * Author: snarkles - http://snarkles.net/ - scripts [at] snarkles [dot] net
  8. * Date: 09-Mar-2004
  9. *
  10. * Feel free to use this on your site, but please leave these comments
  11. * in tact. Thanks. :)
  12. *******************************************************************************/
  13.  
  14. // Convert current time to binary
  15. function binaryize() {
  16. resetPanel();
  17. var time = getCurrentTime();
  18. for (i = 1; i <= time.length; i++) {
  19. num = time.charAt(i - 1);
  20. for (j = 8; j >= 1; j = j / 2) {
  21. if (num - j >= 0) {
  22. eval("document.images.C" + i + j + ".src = 'on.gif'");
  23. num = num - j;
  24. }
  25. }
  26. }
  27. updateClock();
  28. }
  29.  
  30. // Reset panel so all images are off
  31. function resetPanel() {
  32. for (i = 0; i < document.images.length; i++) {
  33. document.images[i].src = "off.gif";
  34. }
  35. }
  36.  
  37. // Get current time, return as 6-digit string: HHMMSS
  38. function getCurrentTime() {
  39. var now = new Date();
  40. var hours = zeropad(now.getHours());
  41. var minutes = zeropad(now.getMinutes());
  42. var seconds = zeropad(now.getSeconds());
  43. var time = hours + "" + minutes + "" + seconds;
  44. return time;
  45. }
  46.  
  47. // Pad numbers < 10 with zeroes
  48. function zeropad(num) {
  49. if (num < 10) {
  50. return "0" + num;
  51. } else {
  52. return num;
  53. }
  54. }
  55.  
  56. // Update the clock every second
  57. function updateClock() {
  58. setTimeout("binaryize()", 1000);
  59. }

Report this snippet 

You need to login to post a comment.