繁体   English   中英

如何使用 javascript 中的按钮从计时器中添加和减去时间

[英]how to add and subtract time from a timer with a button in javascript

我只是在学习 javascript 并玩弄计时器只是为了好玩。 我从 w3schools 拿了一个简单的计时器,现在我尝试添加一个 + 和 - 按钮来添加和减去计时器的时间。 我从加号按钮开始并将其设置为 clearInterval ,这可以正常工作以停止我只是不知道从那里到 go 的时间?

    <!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
p {
  text-align: center;
  font-size: 60px;
  margin-top: 0px;
}
</style>
</head>
<body>

<p id="timer"></p>

<script>
// Set the date we're counting down to
var today = new Date();
var countDownDate = new Date(today.getTime() + (1 * 60 * 60 * 1000));

// Update the count down every 1 second
var x = setInterval(function() {


  // Get today's date and time
  var now = new Date().getTime();
    
  // Find the distance between now and the count down date
  var distance = countDownDate - now;
    
  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);
    
  // Output the result in an element with id="timer"
  document.getElementById("timer").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";
    
  // If the count down is over, remove photo
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("timer").innerHTML = ""; 
  }
}, 1000);
</script>


<p id="timer" onclick="myFunction()">+</p>

<script>
function myFunction() {

clearInterval(x);

  document.getElementById("timer").innerHTML = "New Time";
  
  
}
</script>

</body>
</html> 

如果您只想添加/删除时间,则没有理由清除间隔,只需更新countDownDate其他一切都会继续工作:

 // Set the date we're counting down to var today = new Date(); var countDownDate = new Date(today.getTime() + (1 * 60 * 60 * 1000)); const update = function() { // Get today's date and time var now = new Date().getTime(); // Find the distance between now and the count down date var distance = countDownDate - now; // Time calculations for days, hours, minutes and seconds var days = Math.floor(distance / (1000 * 60 * 60 * 24)); var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((distance % (1000 * 60)) / 1000); // Output the result in an element with id="timer" document.getElementById("timer").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s "; // If the count down is over, remove photo if (distance < 0) { clearInterval(x); document.getElementById("timer").innerHTML = ""; } } // Update the count down every 1 second var x = setInterval(update, 1000); function addDay() { countDownDate.setDate(countDownDate.getDate()+1) update(); } function subDay() { countDownDate.setDate(countDownDate.getDate()-1) update(); }
 p { text-align: center; font-size: 60px; margin-top: 0px; }
 <p id="timer"></p> <button onclick="addDay()">+</button> <button onclick="subDay()">-</button>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM