let countDownDate = new Date(returned_endate.toString()).getTime();

this.timerinterval = setInterval(function() {

  let now = new Date().getTime();
  let distance = countDownDate - now;
  let days = Math.floor(distance / (1000 * 60 * 60 * 24));
  let hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  let seconds = Math.floor((distance % (1000 * 60)) / 1000);
  console.log(now, "now", "countDownDate", countDownDate, "distance", distance, "days", days);
  document.getElementById("demo").innerHTML = "Back In " + hours + "H " +
    minutes + "M " + seconds + "S ";

  if (distance < 0) {

    clearInterval(this.timerinterval);
    document.getElementById("demo").innerHTML = "";
  }
}, 1000);
1
Harikrishnan S 20 Mei 2020, 16:46

1 menjawab

Jawaban Terbaik

Kesalahan menggunakan fungsi kata kunci untuk mendeklarasikan panggilan balik Anda. Gunakan ini sebagai gantinya:

let countDownDate = new Date(returned_endate.toString()).getTime();

this.timerinterval = setInterval(() => {

  let now = new Date().getTime();
  let distance = countDownDate - now;
  let days = Math.floor(distance / (1000 * 60 * 60 * 24));
  let hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  let seconds = Math.floor((distance % (1000 * 60)) / 1000);
  console.log(now, "now", "countDownDate", countDownDate, "distance", distance, "days", days);
  document.getElementById("demo").innerHTML = "Back In " + hours + "H " +
    minutes + "M " + seconds + "S ";

  if (distance < 0) {

    clearInterval(this.timerinterval);
    document.getElementById("demo").innerHTML = "";
  }
}, 1000);
0
Igor 20 Mei 2020, 16:42