JS

HOW TO PAUSE OR SLEEP IN JAVASCRIPT

function demo () {   DO SOMETHING   console.log("First");     WAIT 1 SECOND   let now = Date.now(), end = now + 1000;   while (now < end) { now = Date.now(); }      PROCEED   console.log("Second");  }

LOOP UTILL TIME'S UP

01

function demo () {   DO SOMETHING   console.log("First");     RUN THIS AFTER 1 SECOND   setTimeout(demoA, 1000);  }   function demoA () {   console.log("Second");  }

GOOD OLD TIMER

02

function demo () {   DO SOMETHING   console.log("First");     WAIT 1 SECOND   await new Promise(res =>    { setTimeout(res, 1000); });     PROCEED    console.log("Second");  }

ASYNC TIMEOUT

03