Created
March 9, 2018 23:46
-
-
Save DavidMellul/5f3b9f848c97694b219943bc47e2f36f to your computer and use it in GitHub Desktop.
This file contains 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
var torchRunning = false; | |
var startTime = new Date(); | |
function sleep(milliseconds) { // A basic sleep implementation to "pause" the current thread | |
var start = new Date().getTime(); | |
for (var i = 0; i < 1e7; i++) { | |
if ((new Date().getTime() - start) > milliseconds){ | |
break; | |
} | |
} | |
} | |
function switchLight() { | |
torchRunning = !torchRunning; | |
console.log(torchRunning); | |
} | |
// One light pulsation (ON -> OFF) per 20 ms (10 ms -> ON , 20 ms -> OFF) | |
for(let i = 0; i < 100; i++) // 100 switchLight() -> 50 pulsations | |
{ | |
sleep(10); | |
switchLight(); | |
} | |
var endTime = new Date(); | |
var elapsed = (endTime - startTime) / 1000; // Should be -> 20 x 50 = 1000 ms | |
console.log(elapsed); | |
// => After running this script ten times, here are the results : | |
/* | |
1.103 | |
1.102 | |
1.118 | |
1.105 | |
1.103 | |
1.107 | |
1.112 | |
1.108 | |
1.103 | |
1.112 | |
*/ | |
function isValid(time) { // Ensures 1 second has elapsed | |
return time == 1; | |
} | |
console.log( isValid(elapsed) ? 'Okay, all good':'Wrong :( ' ); | |
// => Wrong :( ten times |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment