將指定秒數轉換爲時分秒格式

例如:後臺返回surplus = 54321 => 15時05分22秒

var second = data.data.surplus;

function setTimes(second) { //獲取到的時間
  timer = setInterval(function () {
    second -= 1;

    var hour = Math.floor(second / (60 * 60));
    var seconds = second % (60 * 60);
    var minute = Math.floor(seconds / 60);
    var se = seconds % 60;
    document.getElementById('hour_show').innerHTML =
      '<span style="color:#FFFFFF;font-size:0.3rem;background:#F56492;border-radius:0.02rem;width:0.2rem;height:0.2rem;">' +
      toD(hour) +
      '</span><i style="color:#F485A8;margin:0.03rem;width:0.24rem;height:0.24rem;font-size:0.24rem;">時</i><span style="color:#FFFFFF;background:#F56492;border-radius:0.02rem;font-size:0.3rem;width:0.2rem;height:0.2rem;">' +
      toD(minute) +
      '</span><i style="color:#F485A8;margin:0.03rem;width:0.24rem;height:0.24rem;font-size:0.24rem;">分</i><span style="color:#FFFFFF;font-size:0.3rem;background:#F56492;border-radius:0.02rem;width:0.2rem;height:0.2rem;">' +
      toD(se) +
      '</span><i style="color:#F485A8;margin:0.03rem;width:0.24rem;height:0.24rem;font-size:0.24rem;">秒</i>'

    if (second === 0) {
      clearInterval(timer)
      window.location.reload();
      return;
    }

  }, 1000)
}

function toD(n) {
  return n < 10 ? '0' + n : n;
}

複製代碼
相關文章
相關標籤/搜索