Skip to content

Commit f3f2921

Browse files
author
Troy Giunipero
committed
fixed digit synchronization with blocks; clock now refreshes when window becomes in focus
1 parent e7da74a commit f3f2921

File tree

2 files changed

+108
-68
lines changed

2 files changed

+108
-68
lines changed

css/style.css

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ body#night {
1616
}
1717

1818
h1 {
19-
margin: 55px;
20-
display: none;
21-
font-size: 60px;
19+
margin: 55px;
20+
display: none;
21+
font-size: 60px;
22+
font-weight: normal;
2223
}
2324

2425
hr {

js/binaryClock.js

Lines changed: 104 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ BINARYCLOCK.start = function () {
1313
// a flag to identify when clock is drawn for the first time
1414
BINARYCLOCK.newDisplay = true;
1515

16+
// get initial time readings
17+
BINARYCLOCK.hours = BINARYCLOCK.getCurrentTime('h');
18+
BINARYCLOCK.minutes = BINARYCLOCK.getCurrentTime('m');
19+
20+
// update clock at second intervals
21+
if (typeof BINARYCLOCK.blockIntervalID === 'number') {
22+
window.clearInterval(BINARYCLOCK.blockIntervalID);
23+
}
1624
BINARYCLOCK.blockIntervalID = window.setInterval(function () {
1725
BINARYCLOCK._update();
1826
}, 1000);
@@ -176,7 +184,7 @@ BINARYCLOCK.toggleMode = function () {
176184
// if no style attribute is provided,
177185
// defaults to 'background-color'
178186
function toggleColor(element, color, attribute) {
179-
if (attribute === undefined) {
187+
if (typeof attribute === 'undefined') {
180188
attribute = 'background-color';
181189
}
182190

@@ -302,7 +310,7 @@ BINARYCLOCK.toggleLayout = function () {
302310

303311
verticalOffset -= 70; // shift up 70px
304312

305-
if ((i+1) % 6 === 0) {
313+
if ((i + 1) % 6 === 0) {
306314
verticalOffset = 330; // reset vertical offset
307315
horizontalOffset += 150; // shift right 150px
308316
}
@@ -326,7 +334,7 @@ BINARYCLOCK.toggleLayout = function () {
326334

327335
horizontalOffset += 100; // shift right 100px
328336

329-
if ((i+1) % 6 === 0) {
337+
if ((i + 1) % 6 === 0) {
330338
verticalOffset += 100; // shift down 100px
331339
horizontalOffset = 0; // reset horizontal offset
332340
}
@@ -344,7 +352,7 @@ BINARYCLOCK.toggleLayout = function () {
344352
// they display according to updated layout
345353
if (timeDigits.display === true) {
346354
layout = BINARYCLOCK.options.layout;
347-
if ( (layout === 'horizontal' && $(window).width() < 1125) ||
355+
if ( (layout === 'horizontal' && $(window).width() < 1125) ||
348356
(layout === 'vertical' && $(window).height() < 545) ) {
349357
$('#timeDisplay').fadeOut(function () {
350358
if (layout === 'horizontal') {
@@ -372,7 +380,7 @@ BINARYCLOCK.toggleLayout = function () {
372380
// check display in case options can't be
373381
// displayed after the layout is toggled
374382
// (applies to title display)
375-
BINARYCLOCK.checkDisplay();
383+
BINARYCLOCK.checkDisplay();
376384
};
377385

378386
/**
@@ -391,7 +399,7 @@ BINARYCLOCK.options.tab.init = function () {
391399
group = svgElem.append('g').style('display', 'none'),
392400
rectElem = group.append('svg:rect'),
393401
textElem = group.append('text');
394-
402+
395403
svgElem
396404
.attr('id', 'optionTab');
397405
rectElem
@@ -406,8 +414,12 @@ BINARYCLOCK.options.tab.init = function () {
406414
.attr('y', 258)
407415
.text('options');
408416

409-
// fades in to avoid flickering
410-
$(group[0]).fadeIn();
417+
// 1 second delay synchronizes tab display
418+
// with blocks upon first time reading
419+
window.setTimeout(function () {
420+
// fades in to avoid flickering
421+
$(group[0]).fadeIn();
422+
}, 1000);
411423
};
412424

413425
/**
@@ -472,7 +484,7 @@ BINARYCLOCK.options.tab.hoverFunctionality = function () {
472484

473485
// hide tab after user hovers outside option panel
474486
window.clearTimeout(timer);
475-
timer = window.setTimeout(function() {
487+
timer = window.setTimeout(function () {
476488
if ( $('#optionControls').css('display') === 'none' ) {
477489
$('#optionTab').stop().animate({'opacity': 0});
478490
}
@@ -481,13 +493,13 @@ BINARYCLOCK.options.tab.hoverFunctionality = function () {
481493
});
482494

483495
// display tab if mouse moves; set timer to hide when still
484-
$(document).mousemove(function() {
496+
$(document).mousemove(function () {
485497

486498
if ( $('#optionControls').css('display') === 'none' ) { // if panel isn't displayed
487499

488500
// set timer when mouse moves for first time on new page
489-
if (timer === undefined) {
490-
timer = window.setTimeout(function() {
501+
if (typeof timer === 'undefined') {
502+
timer = window.setTimeout(function () {
491503
$('#optionTab').stop().animate({'opacity': 0});
492504
}, 4000);
493505
}
@@ -496,7 +508,7 @@ BINARYCLOCK.options.tab.hoverFunctionality = function () {
496508
$('#optionTab').animate({'opacity': 1}); // display it
497509

498510
window.clearTimeout(timer);
499-
timer = window.setTimeout(function() { // reset timer to hide tab in 4s
511+
timer = window.setTimeout(function () { // reset timer to hide tab in 4s
500512
$('#optionTab').stop().animate({'opacity': 0});
501513
}, 4000);
502514
}
@@ -516,7 +528,7 @@ BINARYCLOCK.options.panel.init = function () {
516528
titleDisplay,
517529

518530
// strings for setting up option elements in DOM
519-
titleString,timeDisplayString, numbersOnBlocksString, hrString, modeString, layoutString, controlStrings;
531+
titleString, timeDisplayString, numbersOnBlocksString, hrString, modeString, layoutString, controlStrings;
520532

521533
titleString = '<div><span id="titleBtnLabel">hide</span><button id="titleBtn">title</button></div>';
522534
timeDisplayString = '<div><span id="timeDisplayBtnLabel">show</span><button id="timeDisplayBtn">time display</button></div>';
@@ -660,46 +672,46 @@ BINARYCLOCK.options.timeDigits.start = function () {
660672
var timeDigits = BINARYCLOCK.options.timeDigits,
661673
layout = BINARYCLOCK.options.layout,
662674
triggerTimeUnitDisplay,
663-
timeUnits,
664-
unit;
675+
timeUnits;
665676

666677
timeDigits.display = true;
667678

668679
triggerTimeUnitDisplay = function () {
669680
BINARYCLOCK.timeIntervalID = window.setInterval(function () {
670681

671-
var currentTime, timeSelection;
682+
var timeReading,
683+
timeSelection;
672684

673685
// iterate through the div elements, displaying time for each unit
674686
$.each(timeUnits, function (idx) {
675687

676-
window.setTimeout(function () { // a slight (185 ms) delay
688+
window.setTimeout(function () { // a slight (150 ms) delay
677689
// is introduced, so digits
678690
switch (idx) { // are synchronized with blocks,
679691
case 0: // which fade in and out
680-
unit = 'h';
692+
timeReading = BINARYCLOCK.hours;
693+
timeSelection = $('#h span.time');
681694
break;
682695
case 1:
683-
unit = 'm';
696+
timeReading = BINARYCLOCK.minutes;
697+
timeSelection = $('#m span.time');
684698
break;
685699
case 2:
686-
unit = 's';
700+
timeReading = BINARYCLOCK.seconds;
701+
timeSelection = $('#s span.time');
687702
break;
688703
}
689704

690-
currentTime = BINARYCLOCK.getCurrentTime(unit);
691-
if (currentTime.toString().length < 2) {
692-
currentTime = '0' + currentTime;
705+
if (timeReading.toString().length < 2) {
706+
timeReading = '0' + timeReading;
693707
}
694708

695-
timeSelection = $('#' + unit + ' span.time');
696-
697709
if (timeSelection.text() === '' || // required for start, when no text is displayed
698-
timeSelection.text() != currentTime) { // implicit typecasting
710+
timeSelection.text() != timeReading) { // implicit typecasting
699711

700-
timeSelection.text(currentTime);
712+
timeSelection.text(timeReading);
701713
}
702-
}, 185); // end setTimeout
714+
}, 150); // end setTimeout
703715
});
704716
}, 1000); // end setInterval
705717
};
@@ -751,7 +763,7 @@ BINARYCLOCK.options.timeDigits.stop = function () {
751763
BINARYCLOCK.options.timeDigits.display = false;
752764
window.clearInterval(BINARYCLOCK.timeIntervalID);
753765

754-
$('#timeDisplay').fadeOut('fast', function() {
766+
$('#timeDisplay').fadeOut('fast', function () {
755767
$('#timeDisplay').empty();
756768
});
757769
};
@@ -779,7 +791,7 @@ BINARYCLOCK.checkDisplay = function () {
779791
titleDisplay = BINARYCLOCK.options.titleDisplay;
780792

781793
// hide title if not enough vertical or horizontal space
782-
if ( (layout === 'horizontal' && (height < 590 || width < 650)) ||
794+
if ( (layout === 'horizontal' && (height < 590 || width < 650)) ||
783795
(layout === 'vertical' && (height < 630 || width < 522)) ) {
784796
if (titleDisplay === true) {
785797
$('h1').fadeOut();
@@ -806,7 +818,7 @@ BINARYCLOCK.checkDisplay = function () {
806818
}
807819

808820
// hide time digits if there is not enough space to display them
809-
if ( (layout === 'horizontal' && width < 1125) ||
821+
if ( (layout === 'horizontal' && width < 1125) ||
810822
(layout === 'vertical' && height < 545) ) {
811823
$('#timeDisplay').fadeOut();
812824
// if button in option panel isn't already deactivated...
@@ -894,10 +906,10 @@ BINARYCLOCK.init = (function () {
894906
*/
895907
BINARYCLOCK._update = function () {
896908

897-
var seconds = BINARYCLOCK.getCurrentTime('s'),
898-
minutes = BINARYCLOCK.getCurrentTime('m'),
899-
column,
900-
row;
909+
var column, row;
910+
911+
// update seconds reading
912+
BINARYCLOCK.seconds = BINARYCLOCK.getCurrentTime('s');
901913

902914
/**
903915
* Updates the display for an individual block.
@@ -912,18 +924,18 @@ BINARYCLOCK.init = (function () {
912924
*
913925
* @param row The row index (0 - 2). There are 3 rows from
914926
* top to bottom: hour, minute, and second.
915-
* @param column The specified column of the grid, whereby each column
916-
* represents a quantity of 1, 2, 4, 8, etc.
927+
* @param column The specified column of the grid, whereby each
928+
* column represents a quantity of 1, 2, 4, 8, etc.
917929
*/
918930
function updateBlock(row, column) {
919931

920-
var unit, // 'h', 'm', or 's', depending on the given row
932+
var unit, // the timestamp for hours, minutes or seconds, depending on the given row
921933
groupElements = d3.select('#clockWidget').selectAll('g'); // gets all 18 <g> elements
922934

923935
/**
924936
* Fades a block in or out.
925937
*
926-
* @param toggle Accepts 'in' or 'out' as valid parameters.
938+
* @param toggle Accepts 'in' or 'out' as parameters.
927939
* @param row The row index.
928940
* @param column The column index.
929941
*/
@@ -954,24 +966,24 @@ BINARYCLOCK.init = (function () {
954966
* a block should be activated (i.e. filled in) based
955967
* on its position.
956968
*
957-
* @param unit Accepts 'h', 'm', or 's'.
958-
* @param column The column index.
959-
* @returns Boolean Returns true if the block should
960-
* be activated, false otherwise.
969+
* @param timeReading The time reading for hours, minutes or seconds.
970+
* @param column The column index.
971+
* @returns Boolean Returns true if the block should
972+
* be activated, false otherwise.
961973
*/
962-
function checkBlock(unit, column) {
974+
function checkBlock(timeReading, column) {
963975

964-
var exp = Math.pow(2, column),
965-
inc;
976+
var exp = Math.pow(2, column),
977+
inc;
966978

967-
for (inc = 0; inc < exp; inc += 1) {
968-
if ( BINARYCLOCK.getCurrentTime(unit) % (exp * 2) === (inc + exp) ) {
969-
return true;
970-
}
971-
}
979+
for (inc = 0; inc < exp; inc += 1) {
980+
if ( timeReading % (exp * 2) === (inc + exp) ) {
981+
return true;
982+
}
983+
}
972984

973-
return false;
974-
}
985+
return false;
986+
}
975987

976988
/**
977989
* Toggles the display of a number on a block.
@@ -1001,15 +1013,15 @@ BINARYCLOCK.init = (function () {
10011013
}
10021014

10031015
switch (row) {
1004-
case 0:
1005-
unit = 'h';
1006-
break;
1007-
case 1:
1008-
unit = 'm';
1009-
break;
1010-
case 2:
1011-
unit = 's';
1012-
break;
1016+
case 0:
1017+
unit = BINARYCLOCK.hours;
1018+
break;
1019+
case 1:
1020+
unit = BINARYCLOCK.minutes;
1021+
break;
1022+
case 2:
1023+
unit = BINARYCLOCK.seconds;
1024+
break;
10131025
}
10141026

10151027
if (checkBlock(unit, column)) {
@@ -1039,13 +1051,19 @@ BINARYCLOCK.init = (function () {
10391051
for (column = 5; column >= 0; column -= 1) {
10401052
updateBlock(2, column);
10411053
}
1054+
10421055
// iterate through minute blocks if reading for seconds is 0
1043-
if (seconds === 0) {
1056+
if (BINARYCLOCK.seconds === 0) {
1057+
// update minute reading
1058+
BINARYCLOCK.minutes = BINARYCLOCK.getCurrentTime('m');
10441059
for (column = 5; column >= 0; column -= 1) {
10451060
updateBlock(1, column);
10461061
}
1062+
10471063
// iterate through hour blocks if reading for minutes is 0
1048-
if (minutes === 0) {
1064+
if (BINARYCLOCK.minutes === 0) {
1065+
// update hour reading
1066+
BINARYCLOCK.hours = BINARYCLOCK.getCurrentTime('h');
10491067
for (column = 5; column >= 0; column -= 1) {
10501068
updateBlock(0, column);
10511069
}
@@ -1057,6 +1075,27 @@ BINARYCLOCK.init = (function () {
10571075
// bind clock display rules to window resize event
10581076
$(window).resize(checkDisplay);
10591077

1078+
// stop the clock from updating if the window is not in focus
1079+
$(window).blur(function () {
1080+
window.clearInterval(BINARYCLOCK.blockIntervalID);
1081+
BINARYCLOCK.blockIntervalID = undefined;
1082+
1083+
if (BINARYCLOCK.options.timeDigits.display === true) {
1084+
window.clearInterval(BINARYCLOCK.timeIntervalID);
1085+
BINARYCLOCK.timeIntervalID = undefined;
1086+
}
1087+
});
1088+
1089+
// start updating clock if the window becomes in focus
1090+
$(window).focus(function () {
1091+
BINARYCLOCK.start();
1092+
1093+
if (BINARYCLOCK.options.timeDigits.display === true) {
1094+
$('#timeDisplay').empty();
1095+
BINARYCLOCK.options.timeDigits.start();
1096+
}
1097+
});
1098+
10601099
drawClock();
10611100
prepareOptionsTab();
10621101
prepareOptionPanel();

0 commit comments

Comments
 (0)