how to set a non-async delay in javascript code example
Example 1: wait javascript
wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
Example 2: sleeping in js
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
//using sleep()
sleep(2000); //sleep for 2 seconds