Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment