@@ -13,6 +13,14 @@ BINARYCLOCK.start = function () {
13
13
// a flag to identify when clock is drawn for the first time
14
14
BINARYCLOCK . newDisplay = true ;
15
15
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
+ }
16
24
BINARYCLOCK . blockIntervalID = window . setInterval ( function ( ) {
17
25
BINARYCLOCK . _update ( ) ;
18
26
} , 1000 ) ;
@@ -176,7 +184,7 @@ BINARYCLOCK.toggleMode = function () {
176
184
// if no style attribute is provided,
177
185
// defaults to 'background-color'
178
186
function toggleColor ( element , color , attribute ) {
179
- if ( attribute === undefined ) {
187
+ if ( typeof attribute === ' undefined' ) {
180
188
attribute = 'background-color' ;
181
189
}
182
190
@@ -302,7 +310,7 @@ BINARYCLOCK.toggleLayout = function () {
302
310
303
311
verticalOffset -= 70 ; // shift up 70px
304
312
305
- if ( ( i + 1 ) % 6 === 0 ) {
313
+ if ( ( i + 1 ) % 6 === 0 ) {
306
314
verticalOffset = 330 ; // reset vertical offset
307
315
horizontalOffset += 150 ; // shift right 150px
308
316
}
@@ -326,7 +334,7 @@ BINARYCLOCK.toggleLayout = function () {
326
334
327
335
horizontalOffset += 100 ; // shift right 100px
328
336
329
- if ( ( i + 1 ) % 6 === 0 ) {
337
+ if ( ( i + 1 ) % 6 === 0 ) {
330
338
verticalOffset += 100 ; // shift down 100px
331
339
horizontalOffset = 0 ; // reset horizontal offset
332
340
}
@@ -344,7 +352,7 @@ BINARYCLOCK.toggleLayout = function () {
344
352
// they display according to updated layout
345
353
if ( timeDigits . display === true ) {
346
354
layout = BINARYCLOCK . options . layout ;
347
- if ( ( layout === 'horizontal' && $ ( window ) . width ( ) < 1125 ) ||
355
+ if ( ( layout === 'horizontal' && $ ( window ) . width ( ) < 1125 ) ||
348
356
( layout === 'vertical' && $ ( window ) . height ( ) < 545 ) ) {
349
357
$ ( '#timeDisplay' ) . fadeOut ( function ( ) {
350
358
if ( layout === 'horizontal' ) {
@@ -372,7 +380,7 @@ BINARYCLOCK.toggleLayout = function () {
372
380
// check display in case options can't be
373
381
// displayed after the layout is toggled
374
382
// (applies to title display)
375
- BINARYCLOCK . checkDisplay ( ) ;
383
+ BINARYCLOCK . checkDisplay ( ) ;
376
384
} ;
377
385
378
386
/**
@@ -391,7 +399,7 @@ BINARYCLOCK.options.tab.init = function () {
391
399
group = svgElem . append ( 'g' ) . style ( 'display' , 'none' ) ,
392
400
rectElem = group . append ( 'svg:rect' ) ,
393
401
textElem = group . append ( 'text' ) ;
394
-
402
+
395
403
svgElem
396
404
. attr ( 'id' , 'optionTab' ) ;
397
405
rectElem
@@ -406,8 +414,12 @@ BINARYCLOCK.options.tab.init = function () {
406
414
. attr ( 'y' , 258 )
407
415
. text ( 'options' ) ;
408
416
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 ) ;
411
423
} ;
412
424
413
425
/**
@@ -472,7 +484,7 @@ BINARYCLOCK.options.tab.hoverFunctionality = function () {
472
484
473
485
// hide tab after user hovers outside option panel
474
486
window . clearTimeout ( timer ) ;
475
- timer = window . setTimeout ( function ( ) {
487
+ timer = window . setTimeout ( function ( ) {
476
488
if ( $ ( '#optionControls' ) . css ( 'display' ) === 'none' ) {
477
489
$ ( '#optionTab' ) . stop ( ) . animate ( { 'opacity' : 0 } ) ;
478
490
}
@@ -481,13 +493,13 @@ BINARYCLOCK.options.tab.hoverFunctionality = function () {
481
493
} ) ;
482
494
483
495
// display tab if mouse moves; set timer to hide when still
484
- $ ( document ) . mousemove ( function ( ) {
496
+ $ ( document ) . mousemove ( function ( ) {
485
497
486
498
if ( $ ( '#optionControls' ) . css ( 'display' ) === 'none' ) { // if panel isn't displayed
487
499
488
500
// 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 ( ) {
491
503
$ ( '#optionTab' ) . stop ( ) . animate ( { 'opacity' : 0 } ) ;
492
504
} , 4000 ) ;
493
505
}
@@ -496,7 +508,7 @@ BINARYCLOCK.options.tab.hoverFunctionality = function () {
496
508
$ ( '#optionTab' ) . animate ( { 'opacity' : 1 } ) ; // display it
497
509
498
510
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
500
512
$ ( '#optionTab' ) . stop ( ) . animate ( { 'opacity' : 0 } ) ;
501
513
} , 4000 ) ;
502
514
}
@@ -516,7 +528,7 @@ BINARYCLOCK.options.panel.init = function () {
516
528
titleDisplay ,
517
529
518
530
// strings for setting up option elements in DOM
519
- titleString , timeDisplayString , numbersOnBlocksString , hrString , modeString , layoutString , controlStrings ;
531
+ titleString , timeDisplayString , numbersOnBlocksString , hrString , modeString , layoutString , controlStrings ;
520
532
521
533
titleString = '<div><span id="titleBtnLabel">hide</span><button id="titleBtn">title</button></div>' ;
522
534
timeDisplayString = '<div><span id="timeDisplayBtnLabel">show</span><button id="timeDisplayBtn">time display</button></div>' ;
@@ -660,46 +672,46 @@ BINARYCLOCK.options.timeDigits.start = function () {
660
672
var timeDigits = BINARYCLOCK . options . timeDigits ,
661
673
layout = BINARYCLOCK . options . layout ,
662
674
triggerTimeUnitDisplay ,
663
- timeUnits ,
664
- unit ;
675
+ timeUnits ;
665
676
666
677
timeDigits . display = true ;
667
678
668
679
triggerTimeUnitDisplay = function ( ) {
669
680
BINARYCLOCK . timeIntervalID = window . setInterval ( function ( ) {
670
681
671
- var currentTime , timeSelection ;
682
+ var timeReading ,
683
+ timeSelection ;
672
684
673
685
// iterate through the div elements, displaying time for each unit
674
686
$ . each ( timeUnits , function ( idx ) {
675
687
676
- window . setTimeout ( function ( ) { // a slight (185 ms) delay
688
+ window . setTimeout ( function ( ) { // a slight (150 ms) delay
677
689
// is introduced, so digits
678
690
switch ( idx ) { // are synchronized with blocks,
679
691
case 0 : // which fade in and out
680
- unit = 'h' ;
692
+ timeReading = BINARYCLOCK . hours ;
693
+ timeSelection = $ ( '#h span.time' ) ;
681
694
break ;
682
695
case 1 :
683
- unit = 'm' ;
696
+ timeReading = BINARYCLOCK . minutes ;
697
+ timeSelection = $ ( '#m span.time' ) ;
684
698
break ;
685
699
case 2 :
686
- unit = 's' ;
700
+ timeReading = BINARYCLOCK . seconds ;
701
+ timeSelection = $ ( '#s span.time' ) ;
687
702
break ;
688
703
}
689
704
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 ;
693
707
}
694
708
695
- timeSelection = $ ( '#' + unit + ' span.time' ) ;
696
-
697
709
if ( timeSelection . text ( ) === '' || // required for start, when no text is displayed
698
- timeSelection . text ( ) != currentTime ) { // implicit typecasting
710
+ timeSelection . text ( ) != timeReading ) { // implicit typecasting
699
711
700
- timeSelection . text ( currentTime ) ;
712
+ timeSelection . text ( timeReading ) ;
701
713
}
702
- } , 185 ) ; // end setTimeout
714
+ } , 150 ) ; // end setTimeout
703
715
} ) ;
704
716
} , 1000 ) ; // end setInterval
705
717
} ;
@@ -751,7 +763,7 @@ BINARYCLOCK.options.timeDigits.stop = function () {
751
763
BINARYCLOCK . options . timeDigits . display = false ;
752
764
window . clearInterval ( BINARYCLOCK . timeIntervalID ) ;
753
765
754
- $ ( '#timeDisplay' ) . fadeOut ( 'fast' , function ( ) {
766
+ $ ( '#timeDisplay' ) . fadeOut ( 'fast' , function ( ) {
755
767
$ ( '#timeDisplay' ) . empty ( ) ;
756
768
} ) ;
757
769
} ;
@@ -779,7 +791,7 @@ BINARYCLOCK.checkDisplay = function () {
779
791
titleDisplay = BINARYCLOCK . options . titleDisplay ;
780
792
781
793
// 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 ) ) ||
783
795
( layout === 'vertical' && ( height < 630 || width < 522 ) ) ) {
784
796
if ( titleDisplay === true ) {
785
797
$ ( 'h1' ) . fadeOut ( ) ;
@@ -806,7 +818,7 @@ BINARYCLOCK.checkDisplay = function () {
806
818
}
807
819
808
820
// hide time digits if there is not enough space to display them
809
- if ( ( layout === 'horizontal' && width < 1125 ) ||
821
+ if ( ( layout === 'horizontal' && width < 1125 ) ||
810
822
( layout === 'vertical' && height < 545 ) ) {
811
823
$ ( '#timeDisplay' ) . fadeOut ( ) ;
812
824
// if button in option panel isn't already deactivated...
@@ -894,10 +906,10 @@ BINARYCLOCK.init = (function () {
894
906
*/
895
907
BINARYCLOCK . _update = function ( ) {
896
908
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' ) ;
901
913
902
914
/**
903
915
* Updates the display for an individual block.
@@ -912,18 +924,18 @@ BINARYCLOCK.init = (function () {
912
924
*
913
925
* @param row The row index (0 - 2). There are 3 rows from
914
926
* 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.
917
929
*/
918
930
function updateBlock ( row , column ) {
919
931
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
921
933
groupElements = d3 . select ( '#clockWidget' ) . selectAll ( 'g' ) ; // gets all 18 <g> elements
922
934
923
935
/**
924
936
* Fades a block in or out.
925
937
*
926
- * @param toggle Accepts 'in' or 'out' as valid parameters.
938
+ * @param toggle Accepts 'in' or 'out' as parameters.
927
939
* @param row The row index.
928
940
* @param column The column index.
929
941
*/
@@ -954,24 +966,24 @@ BINARYCLOCK.init = (function () {
954
966
* a block should be activated (i.e. filled in) based
955
967
* on its position.
956
968
*
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.
961
973
*/
962
- function checkBlock ( unit , column ) {
974
+ function checkBlock ( timeReading , column ) {
963
975
964
- var exp = Math . pow ( 2 , column ) ,
965
- inc ;
976
+ var exp = Math . pow ( 2 , column ) ,
977
+ inc ;
966
978
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
+ }
972
984
973
- return false ;
974
- }
985
+ return false ;
986
+ }
975
987
976
988
/**
977
989
* Toggles the display of a number on a block.
@@ -1001,15 +1013,15 @@ BINARYCLOCK.init = (function () {
1001
1013
}
1002
1014
1003
1015
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 ;
1013
1025
}
1014
1026
1015
1027
if ( checkBlock ( unit , column ) ) {
@@ -1039,13 +1051,19 @@ BINARYCLOCK.init = (function () {
1039
1051
for ( column = 5 ; column >= 0 ; column -= 1 ) {
1040
1052
updateBlock ( 2 , column ) ;
1041
1053
}
1054
+
1042
1055
// 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' ) ;
1044
1059
for ( column = 5 ; column >= 0 ; column -= 1 ) {
1045
1060
updateBlock ( 1 , column ) ;
1046
1061
}
1062
+
1047
1063
// 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' ) ;
1049
1067
for ( column = 5 ; column >= 0 ; column -= 1 ) {
1050
1068
updateBlock ( 0 , column ) ;
1051
1069
}
@@ -1057,6 +1075,27 @@ BINARYCLOCK.init = (function () {
1057
1075
// bind clock display rules to window resize event
1058
1076
$ ( window ) . resize ( checkDisplay ) ;
1059
1077
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
+
1060
1099
drawClock ( ) ;
1061
1100
prepareOptionsTab ( ) ;
1062
1101
prepareOptionPanel ( ) ;
0 commit comments