javascript sleep 5 seconds code example
Example 1: js wait for time
function sleep(milliseconds) {
const start = Date.now();
while (Date.now() - start < milliseconds);
}
console.log("Hello");
sleep(2000);
console.log("World!");
Example 2: 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 3: javascript wait 10 seconds
setTimeout(function () {
}, 10000);
.then(() => {
({ timeout: 10000 });
});
Example 4: wait javascript
wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
Example 5: js add delay
await new Promise(resolve => setTimeout(resolve, 1000));
Example 6: javascript sleep settimeout
setTimeout(() => { console.log("World!"); }, 2000);