javascript - 我如何更改它以显示用户在几分钟内输入?

标签 javascript html css

我尝试在几分钟内更改用户输入,但不幸的是,当我尝试这样做时,没有任何改变。

我所做的是将“remseconds”更改为 remminuts,并将数学也更改为“x 60”,但什么也没发生。

const container = document.querySelector('.counter');
const buttonsDiv = document.querySelector('.buttons');
const secInput = document.getElementById('seconds');

var seconds;
var remseconds;
var minuts;
var toCount = false;

function toSubmit() {
  display('start');
  remove('seconds');
  remove('ok');
  seconds = Number(secInput.value);
  counting();
}

function display(e) {
  document.getElementById(e).style.display = 'block';
}

function remove(e) {
  document.getElementById(e).style.display = 'none';
}

function check(stat) {
  if (stat.id == "start") {
    display("stop");
    remove("start");
    toCount = true;
  } else if (stat.id == "stop") {
    display("continue");
    remove("stop");
    toCount = false
  } else {
    display("stop");
    remove("continue");
    toCount = true;
  }
}

function count() {
  if (seconds > 0) {
    if (toCount == true) {
      seconds--;
      remseconds = seconds % 60;
      minuts = Math.floor(seconds / 60);

      if (minuts < 10) {
        minuts = "0" + minuts;
      }

      if (remseconds < 10) {
        remseconds = "0" + remseconds;
      }

      container.innerHTML = minuts + " : " + remseconds;
    }
  } else {
    container.innerHTML = "DONE!";
    buttonsDiv.style.opacity = "0";
  }
}

function counting() {
  remseconds = seconds % 60;
  minuts = Math.floor(seconds / 60);


  if (remseconds < 10) {
    remseconds = "0" + remseconds;
  }

  container.innerHTML = minuts + " : " + remseconds;
  setInterval(count, 1000);
}
<!DOCTYPE html>
<html class="class2">

<head>
  <title>CountDown</title>
  <link rel="stylesheet" type="text/css" href="style.css">
  <link href="https://fonts.googleapis.com/css?family=Poppins&display=swap" rel="stylesheet">
</head>

<body>
  <header>
    <h1>CountDown</h1>
  </header>
  <div class="content">
    <div class="counter"></div>
    <input type="number" id="seconds" placeholder="Seconds">
    <div class="buttons">
      <button class="btn start" id="start" value="1" onclick="check(this)">Start</button>
      <button class="btn start" id="continue" value="1" onclick="check(this)">Continue</button>
      <button class="btn stop" id="stop" value="0" onclick="check(this)">Stop</button>
      <button class="btn start" id="ok" onclick="toSubmit()">Submit</button>
    </div>
  </div>
  <script type="text/javascript" src="main.js"></script>

</body>

</html>

如果您有任何解决方案,请告诉我。

最佳答案

我在这里编辑了几分钟的答案:js seconds countdown

获取分钟并在counting()中仅将其乘以x60转换为秒

const container = document.querySelector('.counter');
const buttonsDiv = document.querySelector('.buttons');
const minInput = document.getElementById('minutes');

var seconds;
var remseconds;
var minuts;
var toCount = false;

function toSubmit() {
  display('start');
  remove('minutes');
  remove('ok');
  minuts= Number(minInput.value);
  counting();
}

function display(e) {
  document.getElementById(e).style.display = 'block';
}

function remove(e) {
  document.getElementById(e).style.display = 'none';
}

function check(stat) {
  if (stat.id == "start") {
    display("stop");
    remove("start");
    toCount = true;
  } else if (stat.id == "stop") {
    display("continue");
    remove("stop");
    toCount = false
  } else {
    display("stop");
    remove("continue");
    toCount = true;
  }
}

function count() {
  if (seconds > 0) {
    if (toCount == true) {
      seconds--;
      remseconds = seconds % 60;
      minuts = Math.floor(seconds / 60);

      if (minuts < 10) {
        minuts = "0" + minuts;
      }

      if (remseconds < 10) {
        remseconds = "0" + remseconds;
      }

      container.innerHTML = minuts + " : " + remseconds;
    }
  } else {
    container.innerHTML = "DONE!";
    buttonsDiv.style.opacity = "0";
  }
}

function counting() {
  seconds = minuts*60;
  remseconds = seconds % 60;
  //minuts = Math.floor(seconds / 60);


  if (remseconds < 10) {
    remseconds = "0" + remseconds;
  }

  container.innerHTML = minuts + " : " + remseconds;
  setInterval(count, 1000);
}
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
}

html, body{
    height: 100%;
}
body{
    width: 100%;
    height: 100%;
    background: linear-gradient(to left top, #0045D6, #00A9f6);
}

header{
    width: 100%;
    height: 13vh;
    background-color: #fff;
    color: #0045F6;
    display: flex;
    justify-content: center;
    align-items: center;
}

.content{
    width: 100%;
    height: 60vh;
    font-size: 3rem;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}

.content #minutes{
    width: 250px;
    font-size: 2rem;
    padding: 1rem;
    outline: none;
    background: none;
    border: none;
    border-bottom: 3px solid #fff;
    color: #fff;
}

#minutes::placeholder{
    color: #ddd;
    font-size: 1.7rem;
}

.btn{
    background-color: #fff;
    border-radius: 4px;
    border: none;
    outline: none;
    cursor: pointer;
    padding: 0.8rem 1.7rem;
    font-size: 1.2rem;
    font-weight: 700;
}

.start{
    color: #1f0;
}

.stop{
    color: #E00;
}

#start, #stop, #continue{
    display: none;
}

.counter{
    color: #fff;
}
 <!DOCTYPE html>
<html class="class2">
<head>
    <title>CountDown</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <link href="https://fonts.googleapis.com/css?family=Poppins&display=swap" rel="stylesheet">
</head>
<body>
    <header>
        <h1>CountDown</h1>
    </header>
    <div class="content">
        <div class="counter"></div>
        <input type="number" id="minutes" placeholder="Minutes">
        <div class="buttons">
            <button class="btn start" id="start" value="1" onclick="check(this)">Start</button>
            <button class="btn start" id="continue" value="1" onclick="check(this)">Continue</button>
            <button class="btn stop" id="stop" value="0" onclick="check(this)">Stop</button>
            <button class="btn start" id="ok" onclick="toSubmit()">Submit</button>
        </div>
    </div>
    <script type="text/javascript" src="main.js"></script>

</body>
</html>

关于javascript - 我如何更改它以显示用户在几分钟内输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60260596/

相关文章:

html - 渐变在 Safari 中不起作用

android - 程式化内容在移动浏览器中缩小

html - 防止工具提示容器上的悬停效果

javascript - 如何在 jQuery 中显示移动耗时?

javascript - 防止浏览器在按下后退按钮时捕捉到上一个滚动位置

javascript - dateDropdown 更改语言属性

c# - 转换印度货币符号

javascript - 如何在 JavaScript 中验证输入?

html - 元素水平堆叠在垂直不堆叠的地方

javascript - 没有输入字段时不会调用 keydown