Created
October 3, 2013 05:26
-
-
Save DmitrySoshnikov/6805408 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* "One second". | |
* | |
* Since 1967, the sample value of One Second is: | |
* | |
* "The duration of 9,192,631,770 periods of the radiation corresponding | |
* to the transition between the two hyperfine levels of the ground state | |
* of the caesium-133 atom." | |
* | |
* by Dmitry Soshnikov <[email protected]> | |
* MIT Style License. | |
*/ | |
// ----------------------------------------------------------- | |
// 1. An atom of Caesium-133. | |
// ----------------------------------------------------------- | |
var Caesium133Atom = { | |
_radiationPeriods: 0, | |
_TRANSITION_RADIATION_PERIODS: 9192631770, | |
// ☢ Let's do some radiation | |
radiate: function() { | |
this._radiationPeriods++; | |
// When we reached this number of radiation periods, | |
// we transit (change energy level). | |
// Ha-ha. Sorry, it will be soooooo long waiting while this number of | |
// periods can be reached in your JS engine, so for real tests | |
// just set _TRANSITION_RADIATION_PERIODS to e.g. 10. | |
if (this._radiationPeriods == this._TRANSITION_RADIATION_PERIODS) { | |
this._transitBetweenTwoHyperfineLevels(); | |
this._radiationPeriods = 0; | |
} | |
setTimeout(this.radiate.bind(this), 0); | |
}, | |
// Out transition time is taken as a Sample value | |
// of One second; inform about it. | |
_transitBetweenTwoHyperfineLevels: function() { | |
this._transitionObserver(); | |
}, | |
registerTransitionObserver: function(transitionObserver) { | |
this._transitionObserver = transitionObserver; | |
} | |
}; | |
Caesium133Atom.radiate(); | |
// ----------------------------------------------------------- | |
// 2. One Second determination process, and time counting. | |
// ----------------------------------------------------------- | |
var seconds = 0; | |
// Connect to caesium-133 atom that will inform us when one | |
// second is passed. | |
Caesium133Atom.registerTransitionObserver(function() { | |
console.log('Time is:', getTime(++seconds)); | |
}); | |
function getTime(seconds) { | |
var minutes = Math.floor(seconds / 60); | |
var hours = Math.floor(minutes / 60); | |
return hours + 'h, ' + minutes + 'm, ' + (seconds % 60); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment