0

I somehow copied this code from stackoverflow, but I forgot the link. I noticed that if I try to input hours, this will be converted into minutes which is very odd to look.

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>

The countdown should display like 02:29:59 after ( 1 second passes ). and not 149:59.

2

1 Answer 1

1

You have forget to calculate hour time.

Try like this ==>

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.