How can js, delay printing the current time?

topic description

it takes a few seconds to print out the current time

related codes

/ / Please paste the code text below (do not replace the code with pictures)
have tried the following ways, but the time printed is the time when the code is running

let time = new Date();
console.log(time);
setTimeout(() => {
    let data = new Date();
    console.log(time);
}, 5000);

what result do you expect? What is the error message actually seen?

I hope to be able to print out the current time in 5 seconds

Mar.03,2022

No problem. What is printed after 5 seconds is the time set by the global variable, that is, the time before 5 seconds.


you console.log (time); that's certainly the same time


modify to:

setTimeout(() => {
 console.log(+new Date());
}, 5000);

or

setTimeout(() => {
  let time = +new Date();
  console.log(time );
}, 5000);
Menu