time.sleep javascript code example
Example 1: javascript sleep
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
async function timeSensativeAction(){
await sleep(5000)
}
Example 2: nodejs promise sleep
await new Promise(r => setTimeout(r, 2000));
Example 3: sleep js
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function demo() {
console.log('Taking a break...');
await sleep(2000);
console.log('Two seconds later, showing sleep in a loop...');
for (let i = 0; i < 5; i++) {
if (i === 3)
await sleep(2000);
console.log(i);
}
}
demo();
Example 4: sleep function js
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
Example 5: node wait 10 seconds
async function init() {
console.log(1);
await sleep(1000);
console.log(2);
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
let sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
Example 6: javascript version of sleep
setTimeout(function(){
alert("Sup!");
}, 2000);