【问题标题】:Show minutes and seconds changing every second in ionic html page在ionic html页面中显示分钟和秒数
【发布时间】:2019-10-02 22:17:43
【问题描述】:

我制作了一个 java-script 倒计时计数器并使用此行显示分钟和秒

document.getElementById("demo").innerHTML = Number(this.minutes) + 'm' + Number(this.seconds) + 's ';

但在 ios 中,由于这条线,它向我显示了 NANm NAN。所以我想用其他方式来显示分钟和秒。

这是我的打字稿代码。

 if ((this.Fajardistance <= '59') && (this.Fajardistance >= '0')) {

    let current_datetime = new Date()
    let formatted_date = current_datetime.getFullYear() + "-" + (current_datetime.getMonth() + 1) + "-" + current_datetime.getDate()
    console.log("formatted_date", formatted_date);

    // Set the date we're counting down to
    var countDownDate = new Date(`${formatted_date.toString()} ${value.fajar_iqamah}`).getTime();        
    console.log("countDownDate", countDownDate);

    // Update the count down every 1 second
    this.interavalTime = setInterval(function () {

      // Get todays date and time
      var now = new Date().getTime();

      // Find the distance between now and the count down date
      var distance = countDownDate - now;

      this.testing = false;
      // Time calculations for days, hours, minutes and seconds
      this.minutes = Math.floor((Number(distance) % (1000 * 60 * 60)) / (1000 * 60));
      this.seconds = Math.floor((Number(distance) % (1000 * 60)) / 1000);

      //In these consoles minutes and seconds are as desired output just want to show them 
      console.log("Minutes:", this.minutes);
      console.log("Seconds:", this.seconds);

      //I was using this line to display minutes and seconds but it's incorrect way to display in ios devices.

      //document.getElementById("demo").innerHTML = Number(this.minutes) + 'm' + Number(this.seconds) + 's ';


      //  // If the count down is over, write some text 
      if (distance < 0) {
        clearInterval(this.interavalTime);

        document.getElementById("demo").style.display = "none";
      }
      //console.log("Inside notification function outside if");


    }, 1000);

  }

主页.Html

<!--<span mode="md" id="demo" style="text-align:right;font-size: 16px; display:inline-block;margin-left: 16px;  margin-top: 0px; color:red;"></span>-->

我之前使用上面注释的方法来显示,但它不是 ios 的正确方法。

【问题讨论】:

    标签: typescript ionic3 angular5


    【解决方案1】:

    你的代码有很多问题。

    1. 什么是 value.fajar_iqamah?
    2. 您正在countDownDate - 现在now 将始终大于 countDown,因此您的间隔只会运行一次。
    3. 即使您将 countDownDate - now 切换为 now - countDownDate,那么距离将始终大于 0,实际上它会一直增加而不是减少。李>

    【讨论】:

    • 1- 是以秒为单位的时间 2- 不,不是更大。正如我告诉你的那样,输出是期望的输出,剩下的就是现在显示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 2023-02-16
    • 1970-01-01
    • 2022-10-01
    相关资源
    最近更新 更多