xxxxxxxxxx
window.setTimeout(function() {
console.log("One second has elapsed");
}, 1000);
xxxxxxxxxx
setTimeout(() => console.log("First"), 1000)
setTimeout(() => console.log("Second"), 1000)
setTimeout(() => console.log("Third"), 1000)
for (let i = 1; i <= 3; i++) {
setTimeout(() => console.log(`#${i}`), 1000)
}
xxxxxxxxxx
function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
delay(1000).then(() => console.log('ran after 1 second1 passed'));
xxxxxxxxxx
setTimeout(myFunction, 3000);
// if you have defined a function named myFunction
// it will run after 3 seconds (3000 milliseconds)
function test1()
{
// let's say JavaScript did have a sleep function..
// sleep for 3 seconds
sleep(3000);
alert('hi');
}
xxxxxxxxxx
async function test() {
console.log('start timer');
await new Promise(resolve => setTimeout(resolve, 1000));
console.log('after 1 second');
}
test();
xxxxxxxxxx
function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
delay(1000).then(() => console.log('ran after 1 second passed'));
xxxxxxxxxx
await new Promise(resolve => setTimeout(resolve, milliseconds))
xxxxxxxxxx
function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
xxxxxxxxxx
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}