xxxxxxxxxx
function wait(sec) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < sec*1000);
}
console.log('waiting')
wait(1)
console.log('i am done')
xxxxxxxxxx
function sleep(milliseconds) {
const start = Date.now();
while (Date.now() - start < milliseconds);
}
console.log("Hello");
sleep(2000);
console.log("World!");
xxxxxxxxxx
var seconds = 30; //Delay or wait 30 seconds
await new Promise((resolve) => setTimeout(resolve, seconds * 1000));
xxxxxxxxxx
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
xxxxxxxxxx
function wait(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
function your_code() {
//code stuff
wait(1000); //waits 1 second before continuing
//other code stuff
}
xxxxxxxxxx
var waitTill = new Date(new Date().getTime() + seconds * 1000);
while(waitTill > new Date()){}
xxxxxxxxxx
function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function myFunction() {
// Code before the delay
console.log("Before delay");
// Delay execution
await wait(2000);
// Code after the delay
console.log("After delay");
}
myFunction();