01 - Introduction to Asynchronous Programming

                
function fun() {
    console.log('Inside Fun');
}

//Creating a delay using a loop
function delay(n) {

    for (let i = 1; i <= n; i++){
        let x = new Date().getTime();
        while (new Date().getTime() < 1000 + x) { } 
    }
    fun();

}

//Write and Run below statement in console window: 
// console.log(delay( Any Number Here ));
// for ex. for 7 seconds of delay, statement will be:
// console.log(delay(7));


//---------------------------------------------------------------------------------------


//setTimeout Function
//====================
// This will be called only once.

console.log("START");

setTimeout(function cb() {
    console.log("CallBack"); // will be printed after 5 sec of delay
}, 5000);

console.log("END");


//--------------------------------------------------------------------------------------------


//setInterval Function : 
//=======================
// This will be called repeatedly after each interval as specified.
// clearInterval(id); // Will clear the timer.
const id = setInterval(function () {
    
    console.log("Called");// will be printed REPEATEDLY after 5 sec of delay
    
}, 5000);
//Write and run below statement to stop
// the timer from printing "Called" after every 5 sec.
// clearInterval(id);