简体   繁体   English

如何减少Javascript中的时间计数器

[英]How to reduce the time counter in Javascript

Need to reduce the timing for the set times. 需要减少设定时间的时间。 Eg user logout time is 14:23:30 means need to show the remaining time seconds to the user. 例如,用户注销时间为14:23:30,则意味着需要向用户显示剩余时间(秒)。 Need to show the counter reducer time in Javascript. 需要在Javascript中显示计数器减少器时间。

Here is a timer. 这是一个计时器。 Like that how to reduce the time from after two minutes. 那样如何减少两分钟后的时间。

<!DOCTYPE html>
<html>
<body>

<p>A script on this page starts this clock:</p>

<p id="demo"></p>

<script>
var myVar = setInterval(myTimer, 1000);

function myTimer() {
    var d = new Date();
    document.getElementById("demo").innerHTML = d.toLocaleTimeString();
}
</script>

</body>
</html>

I guess this is what you are trying to get: 我想这就是您想要得到的:

 var initTime = new Date(); var i = 0; function myTimer(){ i++; var newTime = new Date(initTime.getTime() - i * 1000); document.getElementById("demo").innerHTML = newTime.toLocaleTimeString(); } var myVar = setInterval(myTimer, 1000); 
 <div id="demo"></div> 

If you want to implement timer and want to calculate based on logout time. 如果要实现计时器并要基于注销时间进行计算。

var sampleTimer = (function() {
 var today = new Date(),
 logoutTime = "14:23:30".split(":"),
 logoutTimeInsec = new Date(today.getFullYear(), today.getMonth(), today.getDate(), logoutTime[0], logoutTime[1], logoutTime[2]).getTime();

return  function() {
  var currentTime = new Date().getTime();
  console.log('check');
  if (logoutTimeInsec - currentTime > 0) {
   setTimeout(sampleTimer, 1000);
  }
}

}());

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

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