<!DOCTYPE html>
<title>timer
body {
background: #202020;
color: white;
}
button {
width: 100px;
height: 30px;
border: 3px solid white;
border-radius: 50px;
background: #202020;
color: white;
cursor: pointer;
/*outline: none;*/
}
.font {
color: white;
}
#stopwatch {
font-size: 50px;
position: absolute;
top: 50%;
left: 50%;
/*left-right top-bottom*/
transform: translate(-50%, -60%);
}
#buttons {
position: absolute;
top: 50%;
left: 50%;
/*left-right top-bottom*/
transform: translate(-55%, -1%);
}
#buttons li {
display: inline;
padding-left: 10px;
}
00 : 00 : 00
var timer = document.getElementById('stopwatch');
var today = new Date();
var sec = 0;
var min = 0;
var hr = 0;
var stoptime = true;
function startTimer() {
if (stoptime == true) {
stoptime = false;
timerCycle();
}
setCookie("Nikunj");
}
function stopTimer() {
if (stoptime == false) {
stoptime = true;
}
}
function timerCycle() {
if (stoptime == false) {
sec = parseInt(sec);
min = parseInt(min);
hr = parseInt(hr);
sec = sec + 1;
if (sec == 60) {
min = min + 1;
sec = 0;
}
if (min == 60) {
hr = hr + 1;
min = 0;
sec = 0;
}
if (hr == 24) {
day = day + 1;
hr = 0;
min = 0;
sec = 0;
}
if (hr > 12) {
hr = hr - 12;
}
if (sec <= 9 ) {
sec = '0' + sec;
}
if (min <= 9) {
min = '0' + min;
}
if (hr <= 9) {
hr = '0' + hr;
}
timer.innerHTML =hr +' : ' + min + ' : ' + sec ;
setTimeout("timerCycle()", 1000);
}
}
function resetTimer() {
timer.innerHTML = "00 : 00 : 00";
stoptime = true;
hr = 0;
sec = 0;
min = 0;
}
function setCookie(cname) {
const d = new Date()
let date = new Date().getTime()
d.setTime(d.getTime() + (2*24*60*60*1000));
let expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + date + ";" + expires + ";path=/";
}
function getCookie(cname) {
let name = cname + "=";
let ca = document.cookie.split(';');
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
let startTimer = getCookie("Nikunj");
let dc = new Date().getTime();
if (startTimer != "") {
var difference = dc - startTimer;
var hoursDifference = Math.floor(difference/1000/60/60);
difference -= hoursDifference*1000*60*60
var minutesDifference = Math.floor(difference/1000/60);
difference -= minutesDifference*1000*60
var secondsDifference = Math.floor(difference/1000);
timer.innerHTML = hoursDifference + ' : ' + minutesDifference + ' : ' + secondsDifference;
stoptime = false;
sec = secondsDifference;
min = minutesDifference;
hr = hoursDifference;
timerCycle();
}
}