Javascript Challenges

Timers

We have the following code to show a log in the console after 1000 ms / 1 sec.

var check = false;
var timeStart = new Date().getTime();

setTimeout( function () {
    check = true;
}, 1000 );

while( !check ){
}

console.log( 'Loop has finished', 'Elapsed time:' + (new Date().getTime() - timeStart) );

But for some weird reason it doesn't works as expected and blocks the browser, could you help us?

Exercise

Correct!
False!

Why this is happening?

Exercise

Correct!
False!

Fix the code:

var check = false; var timeStart = new Date().getTime(); setTimeout( function () { check = true; }, 1000 ); while( !check ){ } console.log( 'Loop has finished', 'Elapsed time:' + (new Date().getTime() - timeStart) );