javascript - 在倒计时器javascript中显示小时

标签 javascript php timer

我以某种方式从 stackoverflow 复制了这段代码,但我忘记了链接。我注意到,如果我尝试输入小时,则会将其转换为分钟,这看起来很奇怪。

var hms = "02:30:00";
	var a = hms.split(':'); // split it at the colons

	// minutes are worth 60 seconds. Hours are worth 60 minutes.
	var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); 

		if(seconds > 0)
		{	

	      function secondPassed() {
	      	  
	      	  
	          var minutes = Math.round((seconds - 30)/60),
	              remainingSeconds = seconds % 60;

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

	          document.getElementById('countdown').innerHTML = ":" +minutes + ":" + remainingSeconds;
	          if (seconds == 0) {
	              clearInterval(countdownTimer);
	             //form1 is your form name
	            document.form_quiz.submit();
	          } else {
	              seconds--;
	          }
	      }
	      var countdownTimer = setInterval('secondPassed()', 1000);

	      }
time{

color:red;

}
02:30:00 <br>

<time id="countdown">02:30:00</time>

倒计时应在(1 秒过去)后显示为 02:29:59。而不是 149:59。

最佳答案

You have forget to calculate hour time.

尝试这样==>

var hms = "02:30:00";
	var a = hms.split(':'); // split it at the colons

	// minutes are worth 60 seconds. Hours are worth 60 minutes.
	var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); 

		if(seconds > 0)
		{	

	      function secondPassed() {
	      	  
	      	  
	          var minutes = Math.round((seconds - 30)/60),
	              remainingSeconds = seconds % 60;
                   var hour   =Math.floor((minutes)/60);
                    minutes = minutes%60;

	          if (remainingSeconds < 10) {
	              remainingSeconds = "0" + remainingSeconds;
	          }
              hour = ("0" + hour).slice(-2);
              minutes = ("0" + minutes).slice(-2);
              remainingSeconds= ("0" + remainingSeconds).slice(-2);

	          document.getElementById('countdown').innerHTML = hour +":" +minutes + ":" + remainingSeconds;
	          if (seconds == 0) {
	              clearInterval(countdownTimer);
	             //form1 is your form name
	            document.form_quiz.submit();
	          } else {
	              seconds--;
	          }
	      }
	      var countdownTimer = setInterval('secondPassed()', 1000);

	      }
time{

color:red;

}
02:30:00 <br>

<time id="countdown">02:30:00</time>

关于javascript - 在倒计时器javascript中显示小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44457823/

相关文章:

javascript - 将 JavaScript 变量传递给 PHP - 在 IE6、IE7 和 IE8 中出错

javascript - 如何在 JavaScript 中确认某些内容然后使用

php - 信誉良好的 geo-ip 定位服务

php - 为什么 attach 方法在 Laravel 中返​​回 null?

c - 在Windows上安装定时器/时钟ISR - 单线程环境中的异步调用

javascript - 可变菜单长度

javascript - 使用 jQuery 处理加载可变数量模态的最佳方法是什么?

javascript - 使用 jQuery 在 div 之间导航

c# - 定时器控制增量计数器

java - Java 中的定时器与线程原语