Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

countdown timer javascript stack overflow

function startTimer(duration, display) {
    var timer = duration, minutes, seconds;
    setInterval(function () {
        minutes = parseInt(timer / 60, 10);
        seconds = parseInt(timer % 60, 10);

        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.textContent = minutes + ":" + seconds;

        if (--timer < 0) {
            timer = duration;
        }
    }, 1000);
}

window.onload = function () {
    var fiveMinutes = 60 * 5,
        display = document.querySelector('#time');
    startTimer(fiveMinutes, display);
};
Comment

countdown timer javascript stack overflow

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);

	      }
Comment

countdown timer javascript stack overflow

<html>
<body>
<div id="hms">02:00:00</div>
</body>

<script>
var startTime;
function getCookie(name) {
  var value = "; " + document.cookie;
  var parts = value.split("; " + name + "=");
  if (parts.length == 2) return parts.pop().split(";").shift();
} // credits kirlich @http://stackoverflow.com/questions/10730362/get-cookie-by-name

function count() {
 if(typeof getCookie('remaining')!= 'undefined')
 {
   startTime = getCookie('remaining');
 }
 else if(document.getElementById('hms').innerHTML.trim()!='')
 {
   startTime = document.getElementById('hms').innerHTML;
 }
 else
 {
  var d = new Date(); 
  var h=d.getHours(); 
  var m=d.getMinutes();
  var s=d.getSeconds();
  startTime = h+':'+m+':'+s;
  //OR
  startTime  = d.toTimeString().split(" ")[0]
 }

 var pieces = startTime.split(":");
 var time = new Date();
 time.setHours(pieces[0]);
 time.setMinutes(pieces[1]);
 time.setSeconds(pieces[2]);
 var timediff = new Date(time.valueOf()-1000)
 var newtime = timediff.toTimeString().split(" ")[0];
 document.getElementById('hms').innerHTML=newtime ;
 document.cookie = "remaining="+newtime;
 setTimeout(count,1000);
}
count();
</script>

</html>
Comment

PREVIOUS NEXT
Code Example
Javascript :: convert associative array to json javascript 
Javascript :: yarn incompatible module node 
Javascript :: how to go to another page onclick in react 
Javascript :: javascript tostring method 
Javascript :: run function then empty it javascript 
Javascript :: javascript element onblur 
Javascript :: postman response xml json xml2Json 
Javascript :: bubble sort js 
Javascript :: javascript getcontext 
Javascript :: nodejs for windows 7 
Javascript :: ajax select2 
Javascript :: js loop to array backwards 
Javascript :: starting express server 
Javascript :: javascript how to select radio button 
Javascript :: tailwind config for nextjs 
Javascript :: how to compare objets in an array 
Javascript :: how to remove more than one attribute using jquery 
Javascript :: javascript object without undefined values 
Javascript :: get previous year in javascript 
Javascript :: js char array to string 
Javascript :: usereducer hook 
Javascript :: No provider for ReducerManager 
Javascript :: nodejs fs create file if not exists 
Javascript :: javascript get all days of week 
Javascript :: open modal useState 
Javascript :: how to read a file in javascript 
Javascript :: create an element jquery 
Javascript :: mongodb mongoose aggregate two collections using lookup & format the result set. 
Javascript :: get position of element relative to document 
Javascript :: js subarray 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =