题解 | #倒计时#

倒计时

https://www.nowcoder.com/practice/d86defce50ea45dd92c7a4fbde17d64b

反正就这个思路
function second(second) {
      let day = parseInt(second / 60 / 60 / 24);
      let hour = parseInt((second - day * 86400) / 60 / 60);
      let min = parseInt((second - hour * 60 * 60 - day * 24 * 60 * 60) / 60);
      let seconds = parseInt(
        second - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60
      );
      return { day, hour, min, second: seconds };
    }

    function render(data) {
      for (const key in data) {
        if (Object.hasOwnProperty.call(data, key)) {
          if (data[key].toString().length < 2) {
            data[key] = "0" + data[key];
          }
        }
      }
      const { day, hour, min, second } = data;
      const spans = document.querySelectorAll("#jsCountdown span");
      spans.forEach((item, index) => {
        if (index === 0) {
          +day === 0
            ? item.setAttribute("class", "hide")
            : (item.innerHTML = day + "天");
        }
        if (index === 1) {
          item.innerHTML = hour + ":";
        }
        if (index === 2) {
          item.innerHTML = min + ":";
        }
        if (index === 3) {
          item.innerHTML = second;
        }
      });
    }

全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务