function sleep(milliseconds) {
const start = new Date().getTime();
while ((new Date().getTime() - start) < milliseconds);
}
// performance.now() method returns a high-resolution timestamp in milliseconds
const start = performance.now();
sleep(3000);
const end = performance.now();
const seconds = (end - start) / 1000;
console.log('Execute time: ' + (end - start) + ' milliseconds');
console.log('Execute time: ' + seconds + ' seconds');
/*
run:
Execute time: 2999.9170120004565 milliseconds
Execute time: 2.9999170120004566 seconds
*/