Should you want your Javascript code to attend one (1) second (or extra) whereas executing, then there are a few methods to realize this.
Possibility 1 – Making a delay
Promise
perform delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
delay(1000).then(() => console.log('ran after 1 second elapsed'));
Possibility 2 – Utilizing setTimeout
setTimeout(perform(){
console.log("Prepared")
}, 1000);
Possibility 3 – Utilizing an async
Promise
async perform check() {
console.log('begin timer');
await new Promise(resolve => setTimeout(resolve, 1000));
console.log('after 1 second');
}
check();
Possibility 4 – Making a customized sleep
perform
perform sleep(num) {
let now = new Date();
const cease = now.getTime() + num;
whereas(true) {
now = new Date();
if(now.getTime() > cease) return;
}
}
sleep(1000)
console.log('delay 1 second');