【问题标题】:im making a simple clock which have two buttons 1st for 12 hour format and 2nd for 24 hour format but button wont work when one button is working :JS我正在制作一个简单的时钟,它有两个按钮,第一个是 12 小时格式,第二个是 24 小时格式,但是当一个按钮工作时按钮不起作用:JS
【发布时间】:2022-07-12 01:24:06
【问题描述】:

注意-我是 JS 新手,在这段代码中,24 小时制运行,当我点击 12 小时制格式时 按钮它不会用 24 小时时钟格式代替。基本上按钮不起作用或不起作用 更换时钟格式请帮忙

我使用了一个窗口。运行 24 小时格式时钟的加载功能,当我点击 12 小时格式时钟按钮时,它不会替换

只要运行代码你就会明白我的意思

我想要在这段代码中实现的唯一一件事是一个有两个按钮的时钟第一个:12 小时格式第二个:24 小时格式, 一键更改时钟格式

window.onload = (event) => {
  clock_24();
};

function clock_24() {
  let a;
  let date;
  let time;
  const options = {
    day: 'numeric',
    month: 'long',
    year: 'numeric'
  };
  setInterval(() => {
    a = new Date();
    date = a.toLocaleDateString(undefined, options);
    let hours = String(a.getHours()).padStart(2, '0');
    let min = String(a.getMinutes()).padStart(2, '0');
    let sec = String(a.getSeconds()).padStart(2, '0');
    let milli = a.getMilliseconds();
    time = hours + ':' + min + ':' + sec + ':' + milli;
    document.getElementById('time').innerHTML = time
    document.getElementById('date').innerHTML = date
  }, 1);


  // foramt-change
  document.getElementById("format").innerHTML = "24 Hour format";
  document.getElementById("format").style.fontSize = "32px";
  document.getElementById("format").style.fontfamily = 'Times New Roman';
  console.log('24_loaded');
}


// onclick - event-12-hour-format-clock
// time-12f
function clock_12() {

  setInterval(() => {
    var date = new Date();
    var hh_12 = date.getHours() > 12 ? date.getHours() - 12 : date.getHours();
    var session_12 = date.getHours() >= 12 ? "PM" : "AM";
    hh_12 = hh_12 < 10 ? "0" + hh_12 : hh_12;
    var mm_12 = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    var ss_12 = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    tt_12 = hh_12 + ":" + mm_12 + ":" + ss_12 + " " + session_12;
    document.getElementById('time').innerHTML = tt_12
    // date-12f
    const options = {
      day: 'numeric',
      month: 'long',
      year: 'numeric'
    };
    date = date.toLocaleDateString(undefined, options);
    document.getElementById('date').innerHTML = date
  }, 1000);



  // foramt-change
  document.getElementById("format").innerHTML = "12 Hour format";
  document.getElementById("format").style.fontSize = "32px";
  document.getElementById("format").style.fontfamily = 'Times New Roman';
  console.log('12_loaded');
}
* {
  margin: 0;
  padding: 0;
}

.t-d-cont {
  background-color: gray;
  margin: 50px;
  padding: 50px;
}

#format {
  border-left: 2px solid rgb(70, 166, 239);
  margin-bottom: 20px;
  padding-left: 20px;
  font-weight: bold;
}

.redline {
  border-left: 2px solid red;
  width: 100%;
  padding-left: 20px;
}

.time-box {
  margin-bottom: 50px;
}

#time {
  font-family: 'Rokkitt', serif;
  font-size: 45px;
}

.date-box {
  margin-bottom: 30px;
}

#date {
  font-family: 'Rokkitt', serif;
  font-size: 45px;
}

.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropbtn-2 {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
  margin-left: 15px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- font link -->
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Rokkitt:wght@100&display=swap" rel="stylesheet">

</head>

<body>
  <!-- time and date container  -->
  <div class="t-d-cont">
    <div id="format">
      <h1>24 Hour format</h1>
    </div>
    <div class="redline ">
      <div class="time-box">
        <h1>Current Time: <span id="time"></span></h1>

      </div>
      <div class="date-box">
        <h1> Today's Date: <span id="date"></span></h1>
      </div>
    </div>
    <!-- 24 hour-button -->
    <div class="dropdown">
      <button class="dropbtn-2" onclick="clock_24();">24-hour-format</button>
    </div>
    <!-- 12 hour-button -->
    <div class="dropdown">
      <button class="dropbtn" onclick="clock_12();">12-hour-format</button>
    </div>

  </div>
</body>


</div>


</html>

【问题讨论】:

  • 按钮正常工作。但是您的代码可能会使用一些修剪,因为您在重复自己。间隔也相互干扰。我建议将 24/12 状态置于全局变量中。然后使用一个函数。

标签: javascript html


【解决方案1】:

当您切换时钟时,您需要停止使用以前格式显示的间隔计时器。否则它们都会继续运行,并且其中一个会覆盖另一个。

使用函数外部的变量来保存当前间隔计时器 ID,因此您可以使用 clearInterval() 停止它。

window.onload = (event) => {
  clock_24();
};

let timer;

function clock_24() {
  let a;
  let date;
  let time;
  const options = {
    day: 'numeric',
    month: 'long',
    year: 'numeric'
  };
  clearInterval(timer);
  timer = setInterval(() => {
    a = new Date();
    date = a.toLocaleDateString(undefined, options);
    let hours = String(a.getHours()).padStart(2, '0');
    let min = String(a.getMinutes()).padStart(2, '0');
    let sec = String(a.getSeconds()).padStart(2, '0');
    let milli = a.getMilliseconds();
    time = hours + ':' + min + ':' + sec + ':' + milli;
    document.getElementById('time').innerHTML = time
    document.getElementById('date').innerHTML = date
  }, 1);


  // foramt-change
  document.getElementById("format").innerHTML = "24 Hour format";
  document.getElementById("format").style.fontSize = "32px";
  document.getElementById("format").style.fontfamily = 'Times New Roman';
  console.log('24_loaded');
}


// onclick - event-12-hour-format-clock
// time-12f
function clock_12() {
  clearInterval(timer);
  timer = setInterval(() => {
    var date = new Date();
    var hh_12 = date.getHours() > 12 ? date.getHours() - 12 : date.getHours();
    var session_12 = date.getHours() >= 12 ? "PM" : "AM";
    hh_12 = hh_12 < 10 ? "0" + hh_12 : hh_12;
    var mm_12 = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    var ss_12 = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    tt_12 = hh_12 + ":" + mm_12 + ":" + ss_12 + " " + session_12;
    document.getElementById('time').innerHTML = tt_12
    // date-12f
    const options = {
      day: 'numeric',
      month: 'long',
      year: 'numeric'
    };
    date = date.toLocaleDateString(undefined, options);
    document.getElementById('date').innerHTML = date
  }, 1000);



  // foramt-change
  document.getElementById("format").innerHTML = "12 Hour format";
  document.getElementById("format").style.fontSize = "32px";
  document.getElementById("format").style.fontfamily = 'Times New Roman';
  console.log('12_loaded');
}
* {
  margin: 0;
  padding: 0;
}

.t-d-cont {
  background-color: gray;
  margin: 50px;
  padding: 50px;
}

#format {
  border-left: 2px solid rgb(70, 166, 239);
  margin-bottom: 20px;
  padding-left: 20px;
  font-weight: bold;
}

.redline {
  border-left: 2px solid red;
  width: 100%;
  padding-left: 20px;
}

.time-box {
  margin-bottom: 50px;
}

#time {
  font-family: 'Rokkitt', serif;
  font-size: 45px;
}

.date-box {
  margin-bottom: 30px;
}

#date {
  font-family: 'Rokkitt', serif;
  font-size: 45px;
}

.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropbtn-2 {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
  margin-left: 15px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- font link -->
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Rokkitt:wght@100&display=swap" rel="stylesheet">

</head>

<body>
  <!-- time and date container  -->
  <div class="t-d-cont">
    <div id="format">
      <h1>24 Hour format</h1>
    </div>
    <div class="redline ">
      <div class="time-box">
        <h1>Current Time: <span id="time"></span></h1>

      </div>
      <div class="date-box">
        <h1> Today's Date: <span id="date"></span></h1>
      </div>
    </div>
    <!-- 24 hour-button -->
    <div class="dropdown">
      <button class="dropbtn-2" onclick="clock_24();">24-hour-format</button>
    </div>
    <!-- 12 hour-button -->
    <div class="dropdown">
      <button class="dropbtn" onclick="clock_12();">12-hour-format</button>
    </div>

  </div>
</body>


</div>


</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 2016-08-03
    相关资源
    最近更新 更多