countdown timer javascript stack overflow
JavaScript
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);
};<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>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);
}
Also in JavaScript:
- Title
- how to get tomorrow date in javascript
- Category
- JavaScript
- Title
- create a react native project
- Category
- JavaScript
- Title
- icon shwoing a box react native vector icons
- Category
- JavaScript
- Title
- javascript class click event
- Category
- JavaScript
- Title
- comment obtenir l'élément parent javascript
- Category
- JavaScript
- Title
- get string of element
- Category
- JavaScript
- Title
- count value a to b character javascript
- Category
- JavaScript
- Title
- check if string is datestring javascript
- Category
- JavaScript
- Title
- at leastone checkbox required jquery
- Category
- JavaScript
- Title
- get date one week from now javascript
- Category
- JavaScript
- Title
- ajax laravel example
- Category
- JavaScript
- Title
- add a slash to string in javascript
- Category
- JavaScript
- Title
- HTML5 Accesskey Attribute: you may not need JavaScript to add Keyboard Shortcuts
- Category
- JavaScript
- Title
- block comment js
- Category
- JavaScript
- Title
- if condition in class angular 8
- Category
- JavaScript
- Title
- iterate through list javascript
- Category
- JavaScript
- Title
- array.filter in javascript not working in w3schools
- Category
- JavaScript
- Title
- fonction fleche javascript
- Category
- JavaScript
- Title
- deprecationwarning: mongoose
- Category
- JavaScript
- Title
- javascript array column
- Category
- JavaScript
- Title
- array.splice javascript
- Category
- JavaScript
- Title
- javascript access property values list of objects
- Category
- JavaScript
- Title
- convert number to k m b javascript
- Category
- JavaScript
- Title
- how to get the last element in javascript
- Category
- JavaScript
- Title
- jason rpc reactjs
- Category
- JavaScript
- Title
- how to access data in firebase realtime
- Category
- JavaScript
- Title
- angular onchange event
- Category
- JavaScript
- Title
- get day js
- Category
- JavaScript
- Title
- how to run different node app on server different domains
- Category
- JavaScript
- Title
- edit onclick event
- Category
- JavaScript
- Title
- if you run a script.js with the code, how do you access the value passed to "var" inside script.js ...
- Category
- JavaScript
- Title
- get caret index js
- Category
- JavaScript
- Title
- angular generate component
- Category
- JavaScript
- Title
- iterate object javascript
- Category
- JavaScript
- Title
- compare the triplets hackerrank solution in javascript
- Category
- JavaScript
- Title
- Vue.js force to reload re-render
- Category
- JavaScript
- Title
- iis express gzip
- Category
- JavaScript
- Title
- Angular watching for changes in $http.pendingRequests from directive
- Category
- JavaScript
- Title
- call function javascript
- Category
- JavaScript
- Title
- export default react
- Category
- JavaScript
- Title
- gql TypeError: Object(...) is not a function
- Category
- JavaScript
- Title
- how to add validator to formgroup
- Category
- JavaScript
- Title
- how to remove 000webhost watermark 2019
- Category
- JavaScript
- Title
- constructor in js
- Category
- JavaScript
- Title
- audio get current time
- Category
- JavaScript
- Title
- how to make input type date supported on all browsers
- Category
- JavaScript
- Title
- javascript ajax request
- Category
- JavaScript
- Title
- how to get the size of the window in javascript
- Category
- JavaScript
- Title
- find the last occurrence of a character in a string javascript
- Category
- JavaScript
- Title
- how to make item not dragable in react-sortablejs
- Category
- JavaScript
- Title
- how to parse json
- Category
- JavaScript
- Title
- drupal 8 node has field
- Category
- JavaScript
- Title
- axios delete request payload
- Category
- JavaScript
- Title
- error Unexpected end of JSON input while parsing near '...
- Category
- JavaScript
- Title
- change input placeholder text jquery
- Category
- JavaScript
- Title
- angular lifecycle hooks
- Category
- JavaScript
- Title
- how to find out what a string ends with in javascript
- Category
- JavaScript
- Title
- get last element of array javascript
- Category
- JavaScript
- Title
- for each python json
- Category
- JavaScript
- Title
- async await catch error
- Category
- JavaScript
- Title
- add image in react native
- Category
- JavaScript
- Title
- eval in javascript
- Category
- JavaScript
- Title
- angular check if control has required validator
- Category
- JavaScript
- Title
- Code is valid JSON equivalent of the key/value pair shown that also preserves the original value: UPC: 043875
- Category
- JavaScript
- Title
- how to change color of font in js
- Category
- JavaScript
- Title
- grepper not showing
- Category
- JavaScript
- Title
- change web page on button click
- Category
- JavaScript
- Title
- busca binária em javascript
- Category
- JavaScript
- Title
- custom processing datatables
- Category
- JavaScript
- Title
- javascript Arranging Coins
- Category
- JavaScript
- Title
- can i use hooks with expo in react native
- Category
- JavaScript
- Title
- angular stacked bar
- Category
- JavaScript
- Title
- dynamic forms in react
- Category
- JavaScript
- Title
- and javascript
- Category
- JavaScript
- Title
- for in loops javascript
- Category
- JavaScript
- Title
- how to position View absolute react native
- Category
- JavaScript
- Title
- array javascript django
- Category
- JavaScript
- Title
- javascript automatic click
- Category
- JavaScript
- Title
- font awesome 4.7 cdn
- Category
- JavaScript
- Title
- average of an array js
- Category
- JavaScript
- Title
- angular flex layout
- Category
- JavaScript
- Title
- http server node example
- Category
- JavaScript
- Title
- how to print array backwards
- Category
- JavaScript
- Title
- how to fill false into array javascript
- Category
- JavaScript
- Title
- disabling submit button until all fields have values
- Category
- JavaScript
- Title
- code to launch browser using javascript
- Category
- JavaScript
- Title
- How to get the path to the file that required your module?
- Category
- JavaScript
- Title
- how to store value in session using javascript in php
- Category
- JavaScript
- Title
- how to play sound on load js
- Category
- JavaScript
- Title
- how to get mouse coordinates in javascript
- Category
- JavaScript
- Title
- clear input field value javascript
- Category
- JavaScript
- Title
- how to get the end of an array javascript
- Category
- JavaScript
- Title
- javascript check if code is nodejs
- Category
- JavaScript
- Title
- find all of array which satisfy condition javascript
- Category
- JavaScript
- Title
- create react component class
- Category
- JavaScript
- Title
- get element size javascript
- Category
- JavaScript
- Title
- how to enable click copy function using js
- Category
- JavaScript
- Title
- how to get the index of an array in javascript
- Category
- JavaScript
- Title
- adding border in react native
- Category
- JavaScript
- Title
- how to run p5js in a particular section of page
- Category
- JavaScript