countdown timer javascript stack overflow

Solutions on MaxInterview for countdown timer javascript stack overflow by the best coders in the world

showing results for - "countdown timer javascript stack overflow"
Ilyan
20 Aug 2018
1function startTimer(duration, display) {
2    var timer = duration, minutes, seconds;
3    setInterval(function () {
4        minutes = parseInt(timer / 60, 10);
5        seconds = parseInt(timer % 60, 10);
6
7        minutes = minutes < 10 ? "0" + minutes : minutes;
8        seconds = seconds < 10 ? "0" + seconds : seconds;
9
10        display.textContent = minutes + ":" + seconds;
11
12        if (--timer < 0) {
13            timer = duration;
14        }
15    }, 1000);
16}
17
18window.onload = function () {
19    var fiveMinutes = 60 * 5,
20        display = document.querySelector('#time');
21    startTimer(fiveMinutes, display);
22};
Beatrice
06 Apr 2020
1var hms = "02:30:00";
2	var a = hms.split(':'); // split it at the colons
3
4	// minutes are worth 60 seconds. Hours are worth 60 minutes.
5	var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); 
6
7		if(seconds > 0)
8		{	
9
10	      function secondPassed() {
11	      	  
12	      	  
13	          var minutes = Math.round((seconds - 30)/60),
14	              remainingSeconds = seconds % 60;
15                   var hour   =Math.floor((minutes)/60);
16                    minutes = minutes%60;
17
18	          if (remainingSeconds < 10) {
19	              remainingSeconds = "0" + remainingSeconds;
20	          }
21              hour = ("0" + hour).slice(-2);
22              minutes = ("0" + minutes).slice(-2);
23              remainingSeconds= ("0" + remainingSeconds).slice(-2);
24
25	          document.getElementById('countdown').innerHTML = hour +":" +minutes + ":" + remainingSeconds;
26	          if (seconds == 0) {
27	              clearInterval(countdownTimer);
28	             //form1 is your form name
29	            document.form_quiz.submit();
30	          } else {
31	              seconds--;
32	          }
33	      }
34	      var countdownTimer = setInterval('secondPassed()', 1000);
35
36	      }
Lia
22 Jan 2019
1<html>
2<body>
3<div id="hms">02:00:00</div>
4</body>
5
6<script>
7var startTime;
8function getCookie(name) {
9  var value = "; " + document.cookie;
10  var parts = value.split("; " + name + "=");
11  if (parts.length == 2) return parts.pop().split(";").shift();
12} // credits kirlich @http://stackoverflow.com/questions/10730362/get-cookie-by-name
13
14function count() {
15 if(typeof getCookie('remaining')!= 'undefined')
16 {
17   startTime = getCookie('remaining');
18 }
19 else if(document.getElementById('hms').innerHTML.trim()!='')
20 {
21   startTime = document.getElementById('hms').innerHTML;
22 }
23 else
24 {
25  var d = new Date(); 
26  var h=d.getHours(); 
27  var m=d.getMinutes();
28  var s=d.getSeconds();
29  startTime = h+':'+m+':'+s;
30  //OR
31  startTime  = d.toTimeString().split(" ")[0]
32 }
33
34 var pieces = startTime.split(":");
35 var time = new Date();
36 time.setHours(pieces[0]);
37 time.setMinutes(pieces[1]);
38 time.setSeconds(pieces[2]);
39 var timediff = new Date(time.valueOf()-1000)
40 var newtime = timediff.toTimeString().split(" ")[0];
41 document.getElementById('hms').innerHTML=newtime ;
42 document.cookie = "remaining="+newtime;
43 setTimeout(count,1000);
44}
45count();
46</script>
47
48</html>