Control the max number of divs on page - javascript
I programmed this countdown. How can I control the max number of divs displaying on the page. Example: Thare are 20 countdowns on the page but only 5 are displaying(visible) to user.
EDIT: Now there are 19 countdown displaying on page, how can I set
that thare are max 7 countdown displaying on page?
Here is the code:
<html>
<head>
<meta charset="utf-8">
<title>Tramvaj.info</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<link rel="shortcut icon" type="image/png" href="favi.png" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>
<style type="text/css">
html, body {
height: 100%;
margin: 0;
overflow-x: hidden;
overflow-y: auto;
padding: 0;
width: 100%;
background-image: url("bg.jpg");
background-repeat: no-repeat;
background-position: center center;
background-attachment: fixed;
}
#header_container { background:#1f62a7; border:0px solid #1f62a7; height:35px; left:0; position:relative; width:100%; top:0; }
#headermenu {
width:100%;
height:100%;
position:relative;
top:-8px;
background-color: rgba(255,255,255,0.6);
}
.headermenu {
width: 150px;
height: 26px;
padding-top:15px;
display: block;
float: left;
text-align: center;
text-decoration: none!important;
text-transform: uppercase;
font-family: "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Geneva, Verdana, sans-serif;
font-weight: 400;
font-size: 15px;
letter-spacing: 2px;
color: #ffffff!important;
transition: all .5s ease;
}
#page-wrap { width: 230px; margin: 50px auto; padding: 20px; background: white; -moz-box-shadow: 0 0 20px black; -webkit-box-shadow: 0 0 20px black; box-shadow: 0 0 20px black; }
p { font: 30px/2 Georgia, Serif; margin: 0 0 30px 0; text-indent: 40px; }
a { color: inherit;
text-decoration: none;
}
#dani1Box, #sati1Box, #minute1Box, #sekunde1Box, #dani2Box, #sati2Box, #minute2Box, #sekunde2Box, #dani3Box, #sati3Box, #minute3Box, #sekunde3Box, #dani4Box, #sati4Box, #minute4Box, #sekunde4Box,
#dani5Box, #sati5Box, #minute5Box, #sekunde5Box, #dani6Box, #sati6Box, #minute6Box, #sekunde6Box, #dani7Box, #sati7Box, #minute7Box, #sekunde7Box, #dani8Box, #sati8Box, #minute8Box, #sekunde8Box,
#dani9Box, #sati9Box, #minute9Box, #sekunde9Box, #dani10Box, #sati10Box, #minute10Box, #sekunde10Box, #dani11Box, #sati11Box, #minute11Box, #sekunde11Box, #dani12Box, #sati12Box, #minute12Box, #sekunde12Box, #dani1Box, #sati13Box, #minute13Box, #sekunde13Box, #dani14Box, #sati14Box, #minute14Box, #sekunde14Box, #dani15Box, #sati15Box, #minute15Box, #sekunde15Box, #dani16Box, #sati16Box, #minute16Box, #sekunde16Box, #dani17Box, #sati17Box, #minute17Box, #sekunde17Box, #dani18Box, #sati18Box, #minute18Box, #sekunde18Box, #dani19Box, #sati19Box, #minute19Box, #sekunde19Box, #dani20Box, #sati20Box, #minute20Box, #sekunde20Box{
font-size:25px;
color:#1f62a7;
font-family:Sans-serif;
display: inline-block;
}
</style>
</head>
<body>
<div id="header_container">
<div id="header"></div>
<div align="center">
<div id="headermenu">
SVE STANICE
HOME
</div>
</div>
<div id="page-wrap">
<script>
var d = new Date();
var n = d.getDay();
//if(n == 1 || n == 2 || n == 3 || n == 4 || n == 5){
var timer1;
function cdtd1() {
var sad1 = new Date();
var dolazak1 = new Date(sad1.getFullYear(),sad1.getMonth(),sad1.getDate(),18,00,00);
var timeDiff1 = dolazak1.getTime() - sad1.getTime();
if (timeDiff1 <= 0) {
clearInterval(timer1);
$("#Box1").remove();
}
var sekunde1 = Math.floor(timeDiff1 / 1000);
var minute1 = Math.floor(sekunde1 / 60);
var sati1 = Math.floor(minute1 / 60);
var dani1 = Math.floor(sati1 / 24);
sati1 %= 24;
minute1 %= 60;
sekunde1 %= 60;
$("#dani1Box").html(dani1);
$("#sati1Box").html('7-Dubrava ' + sati1 + ':');
$("#minute1Box").html(minute1 + ':');
$("#sekunde1Box").html(sekunde1);
timer1 = setTimeout(cdtd1, 1000);
}
$(document).ready(function() {
cdtd1();
});
//--------------------------------------------------------------------------------------------------
var timer2;
function cdtd2() {
var sad2 = n
ew Date();
var dolazak2 = new Date(sad2.getFullYear(),sad2.getMonth(),sad2.getDate(),18,00,00);
var timeDiff2 = dolazak2.getTime() - sad2.getTime();
if (timeDiff2 <= 0) {
clearInterval(timer2);
$("#Box2").remove();
}
var sekunde2 = Math.floor(timeDiff2 / 1000);
var minute2 = Math.floor(sekunde2 / 60);
var sati2 = Math.floor(minute2 / 60);
var dani2 = Math.floor(sati2 / 24);
sati2 %= 24;
minute2 %= 60;
sekunde2 %= 60;
$("#dani2Box").html(dani2);
$("#sati2Box").html('7-Dubrava ' + sati2 + ':');
$("#minute2Box").html(minute2 + ':');
$("#sekunde2Box").html(sekunde2);
timer2 = setTimeout(cdtd2, 1000);
}
$(document).ready(function() {
cdtd2();
});
//----------------------------------------------------------------------------------
var timer3;
function cdtd3() {
var sad3 = new Date();
var dolazak3 = new Date(sad3.getFullYear(),sad3.getMonth(),sad3.getDate(),18,00,00);
var timeDiff3 = dolazak3.getTime() - sad3.getTime();
if (timeDiff3 <= 0) {
clearInterval(timer3);
$("#Box3").remove();
}
var sekunde3 = Math.floor(timeDiff3 / 1000);
var minute3 = Math.floor(sekunde3 / 60);
var sati3 = Math.floor(minute3 / 60);
var dani3 = Math.floor(sati3 / 24);
sati3 %= 24;
minute3 %= 60;
sekunde3 %= 60;
$("#dani3Box").html(dani3);
$("#sati3Box").html('7-Dubrava ' + sati3 + ':');
$("#minute3Box").html(minute3 + ':');
$("#sekunde3Box").html(sekunde3);
timer3 = setTimeout(cdtd3, 1000);
}
$(document).ready(function() {
cdtd3();
});
//-----------------------------------------------------------------------------------------
var timer4;
function cdtd4() {
var sad4 = new Date();
var dolazak4 = new Date(sad4.getFullYear(),sad4.getMonth(),sad4.getDate(),18,00,00);
var timeDiff4 = dolazak4.getTime() - sad4.getTime();
if (timeDiff4 <= 0) {
clearInterval(timer4);
$("#Box4").remove();
}
var sekunde4 = Math.floor(timeDiff4 / 1000);
var minute4 = Math.floor(sekunde4 / 60);
var sati4 = Math.floor(minute4 / 60);
var dani4 = Math.floor(sati4 / 24);
sati4 %= 24;
minute4 %= 60;
sekunde4 %= 60;
$("#dani4Box").html(dani4);
$("#sati4Box").html('7-Dubrava ' + sati4 + ':');
$("#minute4Box").html(minute4 + ':');
$("#sekunde4Box").html(sekunde4);
timer4 = setTimeout(cdtd4, 1000);
}
$(document).ready(function() {
cdtd4();
});
var timer5;
function cdtd5() {
var sad5 = new Date();
var dolazak5 = new Date(sad5.getFullYear(),sad5.getMonth(),sad5.getDate(),18,00,00);
var timeDiff5 = dolazak5.getTime() - sad5.getTime();
if (timeDiff5 <= 0) {
clearInterval(timer5);
$("#Box5").remove();
}
var sekunde5 = Math.floor(timeDiff5 / 1000);
var minute5 = Math.floor(sekunde5 / 60);
var sati5 = Math.floor(minute5 / 60);
var dani5 = Math.floor(sati5 / 24);
sati5 %= 24;
minute5 %= 60;
sekunde5 %= 60;
$("#dani5Box").html(dani5);
$("#sati5Box").html('14-Zapruđe ' + sati5 + ':');
$("#minute5Box").html(minute5 + ':');
$("#sekunde5Box").html(sekunde5);
timer5 = setTimeout(cdtd5, 1000);
}
$(document).ready(function() {
cdtd5();
});
var timer6;
function cdtd6() {
var sad6 = new Date();
var dolazak6 = new Date(sad6.getFullYear(),sad6.getMonth(),sad6.getDate(),18,00,00);
var timeDiff6 = dolazak6.getTime() - sad6.getTime();
if (timeDiff6 <= 0) {
clearInterval(timer6);
$("#Box6")
.remove();
}
var sekunde6 = Math.floor(timeDiff6 / 1000);
var minute6 = Math.floor(sekunde6 / 60);
var sati6 = Math.floor(minute6 / 60);
var dani6 = Math.floor(sati6 / 24);
sati6 %= 24;
minute6 %= 60;
sekunde6 %= 60;
$("#dani6Box").html(dani6);
$("#sati6Box").html('7-Dubrava ' + sati6 + ':');
$("#minute6Box").html(minute6 + ':');
$("#sekunde6Box").html(sekunde6);
timer6 = setTimeout(cdtd6, 1000);
}
$(document).ready(function() {
cdtd6();
});
var timer7;
function cdtd7() {
var sad7 = new Date();
var dolazak7 = new Date(sad7.getFullYear(),sad7.getMonth(),sad7.getDate(),18,00,00);
var timeDiff7 = dolazak7.getTime() - sad7.getTime();
if (timeDiff7 <= 0) {
clearInterval(timer7);
$("#Box7").remove();
}
var sekunde7 = Math.floor(timeDiff7 / 1000);
var minute7 = Math.floor(sekunde7/ 60);
var sati7 = Math.floor(minute7 / 60);
var dani7 = Math.floor(sati7 / 24);
sati7 %= 24;
minute7 %= 60;
sekunde7 %= 60;
$("#dani7Box").html(dani7);
$("#sati7Box").html('14-Zapruđe ' + sati7 + ':');
$("#minute7Box").html(minute7 + ':');
$("#sekunde7Box").html(sekunde7);
timer7 = setTimeout(cdtd7, 1000);
}
$(document).ready(function() {
cdtd7();
});
var timer8;
function cdtd8() {
var sad8 = new Date();
var dolazak8 = new Date(sad8.getFullYear(),sad8.getMonth(),sad8.getDate(),18,00,00);
var timeDiff8 = dolazak8.getTime() - sad8.getTime();
if (timeDiff8 <= 0) {
clearInterval(timer8);
$("#Box8").remove();
}
var sekunde8 = Math.floor(timeDiff8 / 1000);
var minute8 = Math.floor(sekunde8 / 60);
var sati8 = Math.floor(minute8 / 60);
var dani8 = Math.floor(sati8 / 24);
sati8 %= 24;
minute8 %= 60;
sekunde8 %= 60;
$("#dani8Box").html(dani8);
$("#sati8Box").html('14-Zapruđe ' + sati8 + ':');
$("#minute8Box").html(minute8 + ':');
$("#sekunde8Box").html(sekunde8);
timer8 = setTimeout(cdtd8, 1000);
}
$(document).ready(function() {
cdtd8();
});
var timer9;
function cdtd9() {
var sad9 = new Date();
var dolazak9 = new Date(sad9.getFullYear(),sad9.getMonth(),sad9.getDate(),18,00,00);
var timeDiff9= dolazak9.getTime() - sad9.getTime();
if (timeDiff9 <= 0) {
clearInterval(timer9);
$("#Box9").remove();
}
var sekunde9 = Math.floor(timeDiff9/ 1000);
var minute9 = Math.floor(sekunde9/ 60);
var sati9= Math.floor(minute9 / 60);
var dani9= Math.floor(sati9/ 24);
sati9 %= 24;
minute9 %= 60;
sekunde9 %= 60;
$("#dani9Box").html(dani9);
$("#sati9Box").html('14-Zapruđe ' + sati9 + ':');
$("#minute9Box").html(minute9 + ':');
$("#sekunde9Box").html(sekunde9);
timer9 = setTimeout(cdtd9, 1000);
}
$(document).ready(function() {
cdtd9();
});
var timer10;
function cdtd10() {
var sad10 = new Date();
var dolazak10 = new Date(sad10.getFullYear(),sad10.getMonth(),sad10.getDate(),18,00,00);
var timeDiff10 = dolazak10.getTime() - sad10.getTime();
if (timeDiff10 <= 0) {
clearInterval(timer10);
$("#Box10").remove();
}
var sekunde10 = Math.floor(timeDiff10/ 1000);
var minute10 = Math.floor(sekunde10 / 60);
var sati10 = Math.floor(minute10 / 60);
var dani10 = Math.floor(sati10/ 24);
sati10 %= 24;
minute10 %= 60;
sekunde10 %= 60;
$("#dani10Box").html(dani10);
$("#sati10Box").html('14-Zapruđe ' + sati10 + ':');
$("#minute10Box").html(minute10 + ':');
$("#sekunde10Box").html(sekunde10);
timer10 = setTimeout(cdtd10, 1000);
}
$(document).ready(function() {
cdtd10();
});
var timer11;
function cdtd11() {
var sad11 = new Date();
var dolazak11 = new Date(sad11.getFullYear(),sad11.getMonth(),sad11.getDate(),18,00,00);
var timeDiff11 = dolazak11.getTime() - sad11.getTime();
if (timeDiff11 <= 0) {
clearInterval(timer11);
$("#Box11").remove();
}
var sekunde11 = Math.floor(timeDiff11 / 1000);
var minute11 = Math.floor(sekunde11 / 60);
var sati11 = Math.floor(minute11 / 60);
var dani11 = Math.floor(sati11 / 24);
sati11 %= 24;
minute11 %= 60;
sekunde11 %= 60;
$("#dani11Box").html(dani11);
$("#sati11Box").html('7-Dubrava ' + sati11 + ':');
$("#minute11Box").html(minute11 + ':');
$("#sekunde11Box").html(sekunde11);
timer11 = setTimeout(cdtd11, 1000);
}
$(document).ready(function() {
cdtd11();
});
var timer12;
function cdtd12() {
var sad12 = new Date();
var dolazak12 = new Date(sad12.getFullYear(),sad12.getMonth(),sad12.getDate(),18,00,00);
var timeDiff12 = dolazak12.getTime() - sad12.getTime();
if (timeDiff12 <= 0) {
clearInterval(timer12);
$("#Box12").remove();
}
var sekunde12 = Math.floor(timeDiff12 / 1000);
var minute12 = Math.floor(sekunde12 / 60);
var sati12 = Math.floor(minute12 / 60);
var dani12 = Math.floor(sati12 / 24);
sati12 %= 24;
minute12 %= 60;
sekunde12 %= 60;
$("#dani12Box").html(dani12);
$("#sati12Box").html('7-Dubrava ' + sati12 + ':');
$("#minute12Box").html(minute12 + ':');
$("#sekunde12Box").html(sekunde12);
timer12 = setTimeout(cdtd12, 1000);
}
$(document).ready(function() {
cdtd12();
});
var timer13;
function cdtd13() {
var sad13 = new Date();
var dolazak13 = new Date(sad13.getFullYear(),sad13.getMonth(),sad13.getDate(),18,00,00);
var timeDiff13 = dolazak13.getTime() - sad13.getTime();
if (timeDiff13 <= 0) {
clearInterval(timer13);
$("#Box13").remove();
}
var sekunde13 = Math.floor(timeDiff13 / 1000);
var minute13 = Math.floor(sekunde13 / 60);
var sati13 = Math.floor(minute13 / 60);
var dani13 = Math.floor(sati13 / 24);
sati13 %= 24;
minute13 %= 60;
sekunde13 %= 60;
$("#dani13Box").html(dani13);
$("#sati13Box").html('7-Dubrava ' + sati13 + ':');
$("#minute13Box").html(minute13 + ':');
$("#sekunde13Box").html(sekunde13);
timer13 = setTimeout(cdtd13, 1000);
}
$(document).ready(function() {
cdtd13();
});
var timer14;
function cdtd14() {
var sad14 = new Date();
var dolazak14 = new Date(sad14.getFullYear(),sad14.getMonth(),sad14.getDate(),18,00,00);
var timeDiff14 = dolazak14.getTime() - sad14.getTime();
if (timeDiff14 <= 0) {
clearInterval(timer14);
$("#Box14").remove();
}
var sekunde14 = Math.floor(timeDiff14 / 1000);
var minute14 = Math.floor(sekunde14 / 60);
var sati14 = Math.floor(minute14 / 60);
var dani14 = Math.floor(sati14 / 24);
sati14 %= 24;
minute14 %= 60;
sekunde14 %= 60;
$("#dani14Box").html(dani14);
$("#sati14Box").html('7-Dubrava ' + sati14 + ':');
$("#minute14Box").html(minute14 + ':');
$("#sekunde14Box").html(sekunde14);
timer14 = setTimeout(cdtd14, 1000);
}
$(document).ready(function() {
cdtd14();
});
var timer15;
function cdtd15() {
var sad15 = new Date();
var dolazak15 = new Date(sad15.getFullYear(),sad15.getMonth(),sad15.getDate(),18,00,00);
var timeDiff15 = dolazak15.getTime() - sad15.getTime();
if (timeDiff15 <= 0) {
clearInterval(timer15);
$("#Box15").remove();
}
var sekunde15 = Math.floor(timeDiff15 / 1000);
var minute15 = Math.floor(sekunde15 / 60);
var sati15 = Math.floor(minute15 / 60);
var dani15 = Math.floor(sati15 / 24);
sati15 %= 24;
minute15 %= 60;
sekunde15 %= 60;
$("#dani15Box").html(dani15);
$("#sati15Box").html('7-Dubrava ' + sati15 + ':');
$("#minute15Box").html(minute15 + ':');
$("#sekunde15Box").html(sekunde15);
timer15 = setTimeout(cdtd15, 1000);
}
$(document).ready(function() {
cdtd15();
});
var timer16;
function cdtd16() {
var sad16 = new Date();
var dolazak16 = new Date(sad16.getFullYear(),sad16.getMonth(),sad16.getDate(),18,00,00);
var timeDiff16 = dolazak16.getTime() - sad16.getTime();
if (timeDiff16 <= 0) {
clearInterval(timer16);
$("#Box16").remove();
}
var sekunde16 = Math.floor(timeDiff16 / 1000);
var minute16 = Math.floor(sekunde16 / 60);
var sati16 = Math.floor(minute16 / 60);
var dani16 = Math.floor(sati16 / 24);
sati16 %= 24;
minute16 %= 60;
sekunde16 %= 60;
$("#dani16Box").html(dani16);
$("#sati16Box").html('7-Dubrava ' + sati16 + ':');
$("#minute16Box").html(minute16 + ':');
$("#sekunde16Box").html(sekunde16);
timer16 = setTimeout(cdtd16, 1000);
}
$(document).ready(function() {
cdtd16();
});
var timer17;
function cdtd17() {
var sad17 = new Date();
var dolazak17 = new Date(sad17.getFullYear(),sad17.getMonth(),sad17.getDate(),18,00,00);
var timeDiff17 = dolazak17.getTime() - sad17.getTime();
if (timeDiff17 <= 0) {
clearInterval(timer17);
$("#Box17").remove();
}
var sekunde17 = Math.floor(timeDiff17 / 1000);
var minute17 = Math.floor(sekunde17 / 60);
var sati17 = Math.floor(minute17 / 60);
var dani17 = Math.floor(sati17 / 24);
sati17 %= 24;
minute17 %= 60;
sekunde17 %= 60;
$("#dani17Box").html(dani17);
$("#sati17Box").html('7-Dubrava ' + sati17 + ':');
$("#minute17Box").html(minute17 + ':');
$("#sekunde17Box").html(sekunde17);
timer17 = setTimeout(cdtd17, 1000);
}
$(document).ready(function() {
cdtd17();
});
var timer18;
function cdtd18() {
var sad18 = new Date();
var dolazak18 = new Date(sad18.getFullYear(),sad18.getMonth(),sad18.getDate(),18,00,00);
var timeDiff18 = dolazak18.getTime() - sad18.getTime();
if (timeDiff18 <= 0) {
clearInterval(timer18);
$("#Box18").remove();
}
var sekunde18 = Math.floor(timeDiff18 / 1000);
var minute18 = Math.floor(sekunde18 / 60);
var sati18 = Math.floor(minute18 / 60);
var dani18 = Math.floor(sati18 / 24);
sati18 %= 24;
minute18 %= 60;
sekunde18 %= 60;
$("#dani18Box").html(dani18);
$("#sati18Box").html('7-Dubrava ' + sati18 + ':');
$("#minute18Box").html(minute18 + ':');
$("#sekunde18Box").html(sekunde18);
timer18 = setTimeout(cdtd18, 1000);
}
$(document).ready(function() {
cdtd18();
});
var timer19;
function cdtd19() {
var sad19 = new Date();
var dolazak19 = new Date(sad19.getFullYear(),sad19.getMonth(),sad19.getDate(),18,00,00);
var timeDiff19 = dolazak19.getTime() - sad19.getTime();
if (timeDiff19 <= 0) {
clearInterval(timer19);
$("#Box19").remove();
}
var sekunde19 = Math.floor(timeDiff19 / 1000);
var minute19 = Math.floor(sekunde19 / 60);
var sati19 = Math.floor(minute19 / 60);
var dani19 = Math.floor(sati19 / 24);
sati19 %= 24;
minute19 %= 60;
sekunde19 %= 60;
$("#dani19Box").html(dani19);
$("#sati19Box").html('7-Dubrava ' + sati19 + ':');
$("#minute19Box").html(minute19 + ':');
$("#sekunde19Box").html(sekunde19);
timer19 = setTimeout(cdtd19, 1000);
}
$(document).ready(function() {
cdtd19();
});
//}
</script>
<center>
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:2px;"> </h1>
<div id="Box1">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati1Box"></div>
<div id="minute1Box"></div>
<div id="sekunde1Box"></div>
</div>
<div id="Box2">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati2Box"></div>
<div id="minute2Box"></div>
<div id="sekunde2Box"></div>
</div>
<div id="Box3">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati3Box"></div>
<div id="minute3Box"></div>
<div id="sekunde3Box"></div>
</div>
<div id="Box4">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati4Box"></div>
<div id="minute4Box"></div>
<div id="sekunde4Box"></div>
</div>
<div id="Box5">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati5Box"></div>
<div id="minute5Box"></div>
<div id="sekunde5Box"></div>
</div>
<div id="Box6">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati6Box"></div>
<div id="minute6Box"></div>
<div id="sekunde6Box"></div>
</div>
<div id="Box7">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati7Box"></div>
<div id="minute7Box"></div>
<div id="sekunde7Box"></div>
</div>
<div id="Box8">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati8Box"></div>
<div id="minute8Box"></div>
<div id="sekunde8Box"></div>
</div>
<div id="Box9">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati9Box"></div>
<div id="minute9Box"></div>
<div id="sekunde9Box"></div>
</div>
<div id="Box10">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati10Box"></div>
<div id="minute10Box"></div>
<div id="sekunde10Box"></div>
</div>
<div id="Box11">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati11Box"></div>
<div id="minute11Box"></div>
<div id="sekunde11Box"></div>
</div>
<div id="Box12">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati12Box"></div>
<div id="minute12Box"></div>
<div id="sekunde12Box"></div>
</div>
<div id="Box13">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati13Box"></div>
<div id="minute13Box"></div>
<div id="sekunde13Box"></div>
</div>
<div id="Box14">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati14Box"></div>
<div id="minute14Box"></div>
<div id="sekunde14Box"></div>
</div>
<div id="Box15">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati15Box"></div>
<div id="minute15Box"></div>
<div id="sekunde15Box"></div>
</div>
<div id="Box16">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati16Box"></div>
<div id="minute16Box"></div>
<div id="sekunde16Box"></div>
</div>
<div id="Box17">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati17Box"></div>
<div id="minute17Box"></div>
<div id="sekunde17Box"></div>
</div>
<div id="Box18">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati18Box"></div>
<div id="minute18Box"></div>
<div id="sekunde18Box"></div>
</div>
<div id="Box19">
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:5px;"> </h1>
<div id="sati19Box"></div>
<div id="minute19Box"></div>
<div id="sekunde19Box"></div>
</div>
<h1 style="font-family:Helvetica;color:#FFFFFF;font-size:2px;"> </h1>
</center>
</div>
</body>
</html>
Thanks
Not sure if I understand what you need to do, but you can always watch the number of DIVs on page with
var divCount = document.getElementsByTagName('div').length;
you can either set display:none on the divs you don't want to display, or in jQuery $('#element').hide();
Related
How does one provide or target different data to DOM elements of one kind and does manage / maintain each of an element's rerender / update cycle?
I'm working on a page on my wordpress site where i pull data from the backend. The data has an end_date column that i use to display a countdown timer i.e. from the current date time till the end_date. This is how i approach it: This is the html element that will display the timer: <div class="t1-auction-timer"></div> This is the javascript that creates the timer: var countDownDate = new Date("<?=$endDate?>").getTime(); var x = setInterval(function() { var now = new Date().getTime(); var distance = countDownDate - now; var days = Math.floor(distance / (1000 * 60 * 60 * 24)); var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((distance % (1000 * 60)) / 1000); var timer = document.querySelectorAll(".t1-auction-timer"); for (var i = 0; i < timer.length; i++) { timer[i].innerHTML = '<ul class="clock-value"><li class="clock-days">' + padNumber(days) + '<small>days</small> </li><li class="clock-separator">:</li><li class="clock-hours"> ' + padNumber(hours) + ' <small>hrs</small> </li><li class="clock-separator">:</li><li class="clock-minutes"> ' + padNumber(minutes) + ' <small>min</small> </li><li class="clock-separator">:</li><li class="clock-seconds"> ' + padNumber(seconds) + ' <small>sec</small> </li></ul>'; } if ( distance < 0 ) { clearInterval(x); for (var i = 0; i < timer.length; i++) { timer[i].innerHTML = "EXPIRED"; } } }, 1000); function padNumber(number) { if( number == 0 ) { number = "0" + number; } else if( number > 9 ) { number; } else { number = "0" + number; } return number; } The <?=$endDate?> variable is passed from php. Its being passed correctly. The number of timers to be displayed will be dynamic depending on the records in the database. The issue I'm facing is: I only get timer created from the first record. Its duplicated across all posts being displayed on the page. I suspect this has to do with my .t1-auction-timer class loop but I haven't figured it out yet.
You can try this approach which uses the data attribute. var auctionTimer = document.getElementsByClassName('t1-auction-timer'); var interval = setInterval(function() { if(auctionTimer.length == 0 ){clearFunction()} for(var i= 0; i < auctionTimer.length; i++){ var endDate = auctionTimer[i].getAttribute('data-end-date') var int = createTimer(endDate, auctionTimer[i]); } },1000) function clearFunction() { clearInterval(interval); } function createTimer(endDate, element){ var countDownDate = new Date(endDate).getTime(); var now = new Date().getTime(); var distance = countDownDate - now; var days = Math.floor(distance / (1000 * 60 * 60 * 24)); var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((distance % (1000 * 60)) / 1000); element.innerHTML = '<ul class="clock-value"><li class="clock-days">' + padNumber(days) + '<small>days</small> </li><li class="clock-separator">:</li><li class="clock-hours"> ' + padNumber(hours) + ' <small>hrs</small> </li><li class="clock-separator">:</li><li class="clock-minutes"> ' + padNumber(minutes) + ' <small>min</small> </li><li class="clock-separator">:</li><li class="clock-seconds"> ' + padNumber(seconds) + ' <small>sec</small> </li></ul>'; if ( distance < 0 ) { if ( element.classList.contains("t1-auction-timer") ){ element.classList.remove("t1-auction-timer") element.classList.add("expierd") expierd() } } } function padNumber(number) { if( number == 0 ) { number = "0" + number; } else if( number > 9 ) { number; } else { number = "0" + number; } return number; } function expierd(){ var expierdDate = document.getElementsByClassName('expierd'); for(var i= 0; i < expierdDate.length; i++){ expierdDate[i].innerHTML = "expierd" } } <div class="t1-auction-timer" data-end-date="2021-01-03 16:15"></div> <div class="t1-auction-timer" data-end-date="2021-01-01 16:15"></div> <div class="t1-auction-timer" data-end-date="2021-01-04 16:15"></div>
An approach which directly uses a lot from the OP's code almost unchanged does, within an interval, process a collection of timer elements by (re)rendering each timer again and again. A timer element's markup base will be the already above (in the comments) mentioned <time/> element where the the OP's endDate will be retrieved from this element's datetime attribute (also again and again) ... function padNumber(number) { return (number >= 10) && number || `0${ number }`; } function updateTimer(nodeList, timer, timerData) { // GUARD if (timer.dataset.isExpired === "true") { return; } const countDownDate = new Date(timer.dateTime).getTime(); const now = new Date().getTime(); const distance = countDownDate - now; const days = Math.floor(distance / (1000 * 60 * 60 * 24)); const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); const seconds = Math.floor((distance % (1000 * 60)) / 1000); if (distance >= 0) { timer.innerHTML = `<ul class="clock-value"> <li class="clock-days">${ padNumber(days) } <small>days</small></li> <li class="clock-hours">${ padNumber(hours) } <small>hrs</small></li> <li class="clock-minutes">${ padNumber(minutes) } <small>min</small></li> <li class="clock-seconds">${ padNumber(seconds) } <small>sec</small></li> </ul>`; } else { timer.innerHTML = "EXPIRED"; timer.dataset.isExpired = "true"; // clear the interval only in case each timer has been expired. if ([ ...nodeList ].every(elmNode => elmNode.dataset.isExpired === "true" )) { clearInterval(timerData.timerId); } } } function updateAllTimers(nodeList, timerData) { nodeList.forEach(timer => updateTimer(nodeList, timer, timerData)); } function initializeAllTimers() { const nodeList = document.querySelectorAll('.t1-auction-timer'); const data = { timerId: null }; updateAllTimers(nodeList, data); data.timerId = setInterval(updateAllTimers, 1000, nodeList, data); } initializeAllTimers(); body { margin: 0; } ul { list-style: none; margin: 0; padding: 0; } li { padding: 2px 10px 3px 10px; } li .auction-name { font-family: courier; } ul.clock-value, ul.clock-value > li { padding: 0; display: inline-block; } ul.clock-value > li { font-family: ui-monospace; } ul.clock-value li:not(:last-child) small:after { content: ":"; display: inline-block; width: .7em; font-weight: bold; text-align: right; } <ul> <li> Auction <span class="auction-name">Foo</span> expires at/in ... <time class="t1-auction-timer" datetime="2020-12-21 22:15">2020-12-21 22:15</time> </li> <li> Auction <span class="auction-name">Bar</span> expires at/in ... <time class="t1-auction-timer" datetime="2020-12-22 03:15">2020-12-22 03:15</time> </li> <li> Auction <span class="auction-name">Baz</span> expires at/in ... <time class="t1-auction-timer" datetime="2020-12-22 11:30">2020-12-22 11:30</time> </li> <li> Auction <span class="auction-name">Biz</span> expires at/in ... <time class="t1-auction-timer" datetime="2020-12-22 14:45">2020-12-22 14:45</time> </li> <li> Auction <span class="auction-name">Buz</span> expires at/in ... <time class="t1-auction-timer" datetime="2020-12-23 10:20">2020-12-23 10:20</time> </li> <li> Auction <span class="auction-name">Fiz</span> expires at/in ... <time class="t1-auction-timer" datetime="2020-12-24 10:55">2020-12-24 10:55</time> </li> <li> Auction <span class="auction-name">Faz</span> expires at/in ... <time class="t1-auction-timer" datetime="2021-01-04 16:15">2021-01-04 16:15</time> </li> </ul> The above approach might be changed towards creating a list of already pre-rendered timer components where each component's update is not anymore based on assigning a huge piece of always newly created HTML markup to a DOM node's innerHTML. In order to enable a more generic / flexible usage of such components, one now can pass a template-string of custom timer-markup as well as a custom "expired" message to the initializing method. In addition any component now gets identified by its data-countdown-component attribute which makes the creation / initialization process independent from any forced or just layout-related CSS class-names. function emptyElementNode(elmNode) { Array .from(elmNode.childNodes) .forEach(node => node.remove()); } function createTemplateFromMarkup(templateMarkup) { const container = document.createElement('div'); container.innerHTML = String(templateMarkup); return container.firstElementChild; } function getTimeMeasureFromMilliseconds(value) { let days = (value / 3600000 / 24); let hours = ((days - Math.floor(days)) * 24); let minutes = ((hours - Math.floor(hours)) * 60); let seconds = ((minutes - Math.floor(minutes)) * 60); days = Math.floor(days); hours = Math.floor(hours); minutes = Math.floor(minutes); seconds = Math.floor(seconds + 0.001); seconds = ((seconds < 60) && seconds) || 0; minutes = ((minutes < 60) && minutes) || 0; hours = ((hours < 24) && hours) || 0; return { days, hours, minutes, seconds }; } function formatMeasure(value) { return ((value >= 10) && value || `0${ value }`); } function createTimerComponent(rootNode, template, expiredMessage) { const expirationDate = new Date(rootNode.dateTime); const [ elmDays, elmHours, elmMinutes, elmSeconds ] = [...template.querySelectorAll([ '[data-countdown-days]', '[data-countdown-hours]', '[data-countdown-minutes]', '[data-countdown-seconds]', ].join(','))]; const component = { rootNode, elmDays, elmHours, elmMinutes, elmSeconds, isExpired: false, expiredMessage, expiresTimestamp: expirationDate.getTime(), }; emptyElementNode(component.rootNode); rootNode.dateTime = expirationDate.toUTCString(); rootNode.appendChild(template); return component; } function updateTimer(componentList, component, timerData) { // GUARD if (component.isExpired) { return; } const { dateNow } = timerData; const { expiresTimestamp } = component; const { elmDays, elmHours, elmMinutes, elmSeconds } = component; const time = (expiresTimestamp - dateNow); if (time >= 0) { const measures = getTimeMeasureFromMilliseconds(time); elmSeconds.textContent = formatMeasure(measures.seconds); elmMinutes.textContent = formatMeasure(measures.minutes); elmHours.textContent = formatMeasure(measures.hours); elmDays.textContent = formatMeasure(measures.days); } else { component.isExpired = true; emptyElementNode(component.rootNode); component.rootNode.textContent = component.expiredMessage; // clear the interval only in case each timer has been expired. if (componentList.every(item => item.isExpired)) { clearInterval(timerData.timerId); } } } function updateAllTimers(componentList, timerData) { timerData.dateNow = Date.now(); componentList.forEach(component => updateTimer(componentList, component, timerData) ); } function initializeAllTimers(templateMarkup, expiredMessage = 'Closed') { const defaultTemplateMarkup = [ '<span>', '<span data-countdown-days></span> <small>days</small>, ', '<span data-countdown-hours></span> <small>hours</small>, ', '<span data-countdown-minutes></span> <small>minutes</small>, ', '<span data-countdown-seconds></span> <small>seconds</small>', '</span>', ].join(''); let template = createTemplateFromMarkup(templateMarkup); if (!template || template.querySelectorAll([ '[data-countdown-days]', '[data-countdown-hours]', '[data-countdown-minutes]', '[data-countdown-seconds]', ].join(',')).length !== 4) { template = createTemplateFromMarkup(defaultTemplateMarkup); } const componentList = [ ...document.querySelectorAll('[data-countdown-component]') ].map(elmNode => createTimerComponent(elmNode, template.cloneNode(true), expiredMessage) ); const data = { timerId: null, dateNow: null, }; updateAllTimers(componentList, data); data.timerId = setInterval(updateAllTimers, 1000, componentList, data); } initializeAllTimers(`<ul class="clock-value"> <li class="clock-days"> <span data-countdown-days></span> <small>days</small> </li> <li class="clock-hours"> <span data-countdown-hours></span> <small>hours</small> </li> <li class="clock-minutes"> <span data-countdown-minutes></span> <small>minutes</small> </li> <li class="clock-seconds"> <span data-countdown-seconds></span> <small>seconds</small> </li> </ul>`, 'Expired'); body { margin: 0; } ul { list-style: none; margin: 0; padding: 0; } li { padding: 2px 10px 3px 10px; } li .auction-name { font-family: courier; } ul.clock-value, ul.clock-value > li { padding: 0; display: inline-block; } ul.clock-value > li { font-family: ui-monospace; } ul.clock-value li:not(:last-child) small:after { content: ":"; display: inline-block; width: .7em; font-weight: bold; text-align: right; } <ul> <li> Auction <span class="auction-name">Foo</span> expires at/in ... <time data-countdown-component datetime="2020-12-21 22:15">2020-12-21 22:15</time> </li> <li> Auction <span class="auction-name">Bar</span> expires at/in ... <time data-countdown-component datetime="2020-12-22 03:15">2020-12-22 03:15</time> </li> <li> Auction <span class="auction-name">Baz</span> expires at/in ... <time data-countdown-component datetime="2020-12-22 11:30">2020-12-22 11:30</time> </li> <li> Auction <span class="auction-name">Biz</span> expires at/in ... <time data-countdown-component datetime="2020-12-22 14:45">2020-12-22 14:45</time> </li> <li> Auction <span class="auction-name">Buz</span> expires at/in ... <time data-countdown-component datetime="2020-12-23 10:20">2020-12-23 10:20</time> </li> <li> Auction <span class="auction-name">Fiz</span> expires at/in ... <time data-countdown-component datetime="2020-12-24 10:55">2020-12-24 10:55</time> </li> <li> Auction <span class="auction-name">Faz</span> expires at/in ... <time data-countdown-component datetime="2021-01-04 16:15">2021-01-04 16:15</time> </li> </ul>
Countdown timer is restarting evertime I hit refresh
Whenever I refresh my browser its restarting from the start. I uploaded it on server..I'm getting the same result. Here's my code: function getTimeRemaining(endtime) { var t = Date.parse(endtime) - Date.parse(new Date()); var seconds = Math.floor((t / 1000) % 60); var minutes = Math.floor((t / 1000 / 60) % 60); var hours = Math.floor((t / (1000 * 60 * 60)) % 24); var days = Math.floor(t / (1000 * 60 * 60 * 24)); return { 'total': t, 'days': days, 'hours': hours, 'minutes': minutes, 'seconds': seconds }; } function initializeClock(id, endtime) { var clock = document.getElementById(id); var daysSpan = clock.querySelector('.days'); var hoursSpan = clock.querySelector('.hours'); var minutesSpan = clock.querySelector('.minutes'); var secondsSpan = clock.querySelector('.seconds'); function updateClock() { var t = getTimeRemaining(endtime); daysSpan.innerHTML = t.days; hoursSpan.innerHTML = ('0' + t.hours).slice(-2); minutesSpan.innerHTML = ('0' + t.minutes).slice(-2); secondsSpan.innerHTML = ('0' + t.seconds).slice(-2); if (t.total <= 0) { clearInterval(timeinterval); } } updateClock(); var timeinterval = setInterval(updateClock, 1000); } var deadline = new Date(Date.parse(new Date()) + 33 * 24 * 60 * 60 * 1000); initializeClock('clockdiv', deadline); #clockdiv { font-family: Calibri; color: #001a00; display: inline-block; font-weight: 10; text-align: center; font-size: 30px; } #clockdiv>div { padding: 10px; border-radius: 3px; display: inline-block; } .smalltext { padding-top: 5px; font-size: 16px; } <div id="clockdiv"> <div> <span class="days"></span> <div class="smalltext">Days</div> </div> <div> <span class="hours"></span> <div class="smalltext">Hours</div> </div> <div> <span class="minutes"></span> <div class="smalltext">Minutes</div> </div> <div> <span class="seconds"></span> <div class="smalltext">Seconds</div> </div> </div> Here's the fiddle- code What am I doing wrong here? At first I thought it'll be fine if I upload it on server..but still getting the same result. If there's another code..please help me out or guide me..how to do it! Thanks!
It is because of this line var deadline = new Date(Date.parse(new Date()) + 33 * 24 * 60 * 60 * 1000); You keep setting the date to 33 days in the future, so every time the page loads this is set. If you change this line to the following and pass in your desired date. var deadline = new Date(Date.parse('2017-10-30')); Run code snippet below. function getTimeRemaining(endtime) { var t = Date.parse(endtime) - Date.parse(new Date()); var seconds = Math.floor((t / 1000) % 60); var minutes = Math.floor((t / 1000 / 60) % 60); var hours = Math.floor((t / (1000 * 60 * 60)) % 24); var days = Math.floor(t / (1000 * 60 * 60 * 24)); return { 'total': t, 'days': days, 'hours': hours, 'minutes': minutes, 'seconds': seconds }; } function initializeClock(id, endtime) { var clock = document.getElementById(id); var daysSpan = clock.querySelector('.days'); var hoursSpan = clock.querySelector('.hours'); var minutesSpan = clock.querySelector('.minutes'); var secondsSpan = clock.querySelector('.seconds'); function updateClock() { var t = getTimeRemaining(endtime); daysSpan.innerHTML = t.days; hoursSpan.innerHTML = ('0' + t.hours).slice(-2); minutesSpan.innerHTML = ('0' + t.minutes).slice(-2); secondsSpan.innerHTML = ('0' + t.seconds).slice(-2); if (t.total <= 0) { clearInterval(timeinterval); } } updateClock(); var timeinterval = setInterval(updateClock, 1000); } var deadline = new Date(Date.parse('2017-10-30')); initializeClock('clockdiv', deadline); #clockdiv { font-family: Calibri; color: #001a00; display: inline-block; font-weight: 10; text-align: center; font-size: 30px; } #clockdiv>div { padding: 10px; border-radius: 3px; display: inline-block; } .smalltext { padding-top: 5px; font-size: 16px; } <div id="clockdiv"> <div> <span class="days"></span> <div class="smalltext">Days</div> </div> <div> <span class="hours"></span> <div class="smalltext">Hours</div> </div> <div> <span class="minutes"></span> <div class="smalltext">Minutes</div> </div> <div> <span class="seconds"></span> <div class="smalltext">Seconds</div> </div> </div>
When you set var deadline = new Date(Date.parse(new Date()) + 33 * 24 * 60 * 60 * 1000); every time you load the script, it will always be 33 days (and so on) from now. So you'll have to give the script a fixed deadline like so: var deadline = new Date(Date.parse('2017-10-12'));
Upcounter with Momentjs
My problem is that my upcounter lost 1 day. if i set the endDateTime to 1 year and 1 day before or after today it show the right time, but if i set it to exactly 1 year before today it shows the wrong day and month. And if i put the code of updateTimer to fiddlejs.net and test it if it shows the right time than is everything right. sorry for my bad english :) function updateTimer() { //change endDateTime to your actual time minus 1 year var endDateTime = new moment("2016-04-03 4:00:00"); var startDateTime = moment(); var timeLeft = endDateTime + startDateTime; var timeLeft = startDateTime.diff(endDateTime, 'milliseconds', true); var years = Math.floor(moment.duration(timeLeft).asYears()); startDateTime = startDateTime.subtract(years, 'years'); timeLeft = startDateTime.diff(endDateTime, 'milliseconds', true); var months = Math.floor(moment.duration(timeLeft).asMonths()); startDateTime = startDateTime.subtract(months, 'months'); timeLeft = startDateTime.diff(endDateTime, 'milliseconds', true); var days = Math.floor(moment.duration(timeLeft).asDays()); startDateTime = startDateTime.subtract(days, 'days'); timeLeft = startDateTime.diff(endDateTime, 'milliseconds', true); var hours = Math.floor(moment.duration(timeLeft).asHours()); startDateTime = startDateTime.subtract(hours, 'hours'); timeLeft = startDateTime.diff(endDateTime, 'milliseconds', true); var minutes = Math.floor(moment.duration(timeLeft).asMinutes()); startDateTime = startDateTime.subtract(minutes, 'minutes'); timeLeft = startDateTime.diff(endDateTime, 'milliseconds', true); var seconds = Math.floor(moment.duration(timeLeft).asSeconds()); var total = timeLeft; if (years < 10) years = "0" + years; if (months < 10) months = "0" + months; if (days < 10) days = "0" + days; if (hours < 10) hours = "0" + hours; if (minutes < 10) minutes = "0" + minutes; if (seconds < 10) seconds = "0" + seconds; return { 'years': years, 'months': months, 'days': days, 'hours': hours, 'minutes': minutes, 'seconds': seconds, 'total': total, } } function animateClock(span) { span.className = "turn"; setTimeout(function() { span.className = ""; }, 700); } function initializeClock(id, endtime) { var clock = document.getElementById(id); var yearsSpan = clock.querySelector('.years'); var monthsSpan = clock.querySelector('.months'); var daysSpan = clock.querySelector('.days'); var hoursSpan = clock.querySelector('.hours'); var minutesSpan = clock.querySelector('.minutes'); var secondsSpan = clock.querySelector('.seconds'); function updateClock() { var t = updateTimer(); yearsSpan.innerHTML = t.years; monthsSpan.innerHTML = ('0' + t.months).slice(-2); daysSpan.innerHTML = ('0' + t.days).slice(-2); hoursSpan.innerHTML = ('0' + t.hours).slice(-2); minutesSpan.innerHTML = ('0' + t.minutes).slice(-2); secondsSpan.innerHTML = ('0' + t.seconds).slice(-2); //animation // var daysMonth = moment(t.years-t.months-t.days).daysInMonth('month'); var spans = clock.getElementsByTagName("span"); animateClock(spans[5]); if (t.seconds == 0) animateClock(spans[4]); if (t.minutes == 59 && t.seconds == 59) animateClock(spans[3]); if (t.hours == 23 && t.minutes == 59 && t.seconds == 59) animateClock(spans[2]); //if (t.months == daysMonth && t.hours == 23 && t.minutes == 59 && t.seconds == 59) animateClock(spans[1]); // if (t.total <= 0) { // clearInterval(timeinterval); // } } updateClock(); var timeinterval = setInterval(updateClock, 1000); } var deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000); initializeClock('clockdiv', deadline); #clockdiv { font-family: sans-serif; color: #2DA2DB; display: inline-block; font-weight: 100; text-align: center; font-size: 30px; } #clockdiv>div { margin-bottom: 5px; padding: 6px; border-radius: 3px; background: #991b1f; display: inline-block; border-style: solid; border-color: black; } #clockdiv>div:nth-child(2) { background: #aa1e23; } #clockdiv>div:nth-child(3) { background: #bc2025; } #clockdiv>div:nth-child(4) { background: #cc2027; } #clockdiv>div:nth-child(5) { background: #dc1f26; } #clockdiv>div:nth-child(6) { background: #ec1e24; } #clockdiv div>span { font-size: 30px; letter-spacing: 3px; font-family: 'pixel', monospace; color: #white; text-shadow: -2px -2px 0 #000, 2px -2px 0 #000, -2px 2px 0 #000, 2px 2px 0 #000; padding: 6px; border-radius: 3px; background: #333; display: inline-block; border-style: solid; border-color: black; } <script src="http://momentjs.com/downloads/moment.js"></script> <div id="clockdiv"> <div> <span class="years"></span> <div class="smalltext">Jahre</div> </div> <div> <span class="months"></span> <div class="smalltext">Monate</div> </div> <div> <span class="days"></span> <div class="smalltext">Tage</div> </div> <div> <span class="hours"></span> <div class="smalltext">Stunden</div> </div> <div> <span class="minutes"></span> <div class="smalltext">Minuten</div> </div> <div> <span class="seconds"></span> <div class="smalltext">Sekunden</div> </div> </div>
Functions button effects another function
I am trying to add multiple stop watches on a page for tracking tracking how much time it takes users to complete tasks. I have a function that works, I copied it and tried to make a second function duplicate with certain things change the buttons effect the other function. Any help would be appreciated. <script> var time = 0; var running = 0; function startPause() { if(running == 0){ running = 1; increment(); document.getElementById("startPause").innerHTML = "Pause"; }else{ running = 0; document.getElementById("startPause").innerHTML = "Resume"; } } function reset() { running = 0; time = 0; document.getElementById("startPause").innerHTML = "Start"; document.getElementById("output").innerHTML = "00:00:00"; } function increment() { if(running == 1){ setTimeout(function(){ time++; var mins = Math.floor(time/10/60); var secs = Math.floor(time/10 % 60); var tenths = time % 10; if(mins < 10){ mins = "0" + mins; } if(secs < 10){ secs = "0" + secs; } document.getElementById("output").innerHTML = mins + ":" + secs + ":" + "0" + tenths; increment(); },100); } } </script> <script> var time = 0; var running = 0; function startPause2() { if(running == 0){ running = 1; increment2(); document.getElementById("startPause2").innerHTML = "Pause"; }else{ running = 0; document.getElementById("startPause2").innerHTML = "Resume"; } } function reset2() { running = 0; time = 0; document.getElementById("startPause2").innerHTML = "Start"; document.getElementById("output2").innerHTML = "00:00:00"; } function increment2() { if(running == 1){ setTimeout(function(){ time++; var mins = Math.floor(time/10/60); var secs = Math.floor(time/10 % 60); var tenths = time % 10; if(mins < 10){ mins = "0" + mins; } if(secs < 10){ secs = "0" + secs; } document.getElementById("output2").innerHTML = mins + ":" + secs + ":" + "0" + tenths; increment(); },100); } } </script> html { size: 100%,100%; background-color: #f2f2f2; } h1{ font-Family: Arial; color: #5573A9; font-size: 40px; position: static; } body { display: block; margin:8px, dotted line; background-color: #f2f2f2; } #output{ position:center; width:120px; height:25px; background-color:#CCC; border:3px solid #999; } #output2{ position: relative; width:120px; height:25px; background-color:#CCC; border:3px solid #999; } table { display:table; border-collapse:seperate; border-spacing: 52px; border-color: #FFF; } <html> <head> <link rel="stylesheet" type="text/css" href="style.css"> <title>Time Study</title> <h1>Time Study</h1> </head> <body> <table> <tr> <th>Bin</th> <th>Bulk</th> </tr> <tr> <td> <p id="output"></p> <div id="controls"> <button id="startPause" onclick="startPause()">Start</button> <button onclick="reset()">Reset</button> </div> </td> <td> <p id="output2"></p> <div id="controls2"> <button id="startPause2" onclick="startPause2()">Start</button> <button onclick="reset2()">Reset</button> </div> </td> </tr> </table> </body> </html>
How to show minute, second, hour in time progress bar
I have a time progress bar. I use this code. In this time bar, I get the time in seconds, but I want to show minute, second, hour in the time progress bar. var timer = 0, timeTotal = 2500, timeCount = 20, timeStart = 0, cFlag; function updateProgress(percentage) { var x = (percentage/timeTotal)*100, y = x.toFixed(3); $('#pbar_innerdiv').css("width", x + "%"); $('#pbar_innertext').css("left", x + "%").text((percentage / 1000).toFixed(2) + "\00a0s"); } function animateUpdate() { var perc = new Date().getTime() - timeStart; if(perc < timeTotal) { updateProgress(perc); timer = setTimeout(animateUpdate, timeCount); } else { updateProgress(timeTotal); } } $(document).ready(function() { $('#pbar_outerdiv').click(function() { if (cFlag == undefined) { clearTimeout(timer); cFlag = true; timeStart = new Date().getTime(); animateUpdate(); } else if (!cFlag) { cFlag = true; animateUpdate(); } else { clearTimeout(timer); cFlag = false; } }); }); jsfiddle.net/McNetic/hnfRe/397
You can try this. DEMO LINK HERE <div id="pbar_outerdiv" style="width: 300px; height: 20px; border: 1px solid grey; z-index: 1; position: relative; border-radius: 5px; -moz-border-radius: 5px;"> <div id="pbar_innerdiv" style="background-color: lightblue; z-index: 2; height: 100%; width: 0%;"></div> <div id="pbar_innertext" style="z-index: 3; position: absolute; top: 0; left: 0; height: 100%; color: black; font-weight: bold; text-align: center;">0 s</div> </div> <p>Click once to start!</p> <p>Click again to toggle Start/Stop</p> JS... var timer = 0, timeTotal = 250000, timeCount = 20, timeStart = 0, cFlag; function updateProgress(percentage) { var x = (percentage/timeTotal)*100, y = x.toFixed(3); var totalSec= (percentage / 1000); var min = parseInt(totalSec/60); var sec = parseInt(totalSec%60); var hr= parseInt(min/60); min = parseInt(min % 60); $('#pbar_innerdiv').css("width", x + "%"); $('#pbar_innertext').css("left", x + "%").text(hr+":"+min+":"+sec + ""); } function animateUpdate() { var perc = new Date().getTime() - timeStart; if(perc < timeTotal) { updateProgress(perc); timer = setTimeout(animateUpdate, timeCount); } else { updateProgress(timeTotal); } } $(document).ready(function() { $('#pbar_outerdiv').click(function() { if (cFlag == undefined) { clearTimeout(timer); cFlag = true; timeStart = new Date().getTime(); animateUpdate(); } else if (!cFlag) { cFlag = true; animateUpdate(); } else { clearTimeout(timer); cFlag = false; } }); }); CSS... #pbar_outerdiv { cursor: pointer; }
With percentaje (line 11): function updateProgress(percentage) { var x = (percentage/timeTotal)*100, y = x.toFixed(3); $('#pbar_innerdiv').css("width", x + "%"); $('#pbar_innertext').css("left", x + "%").text(x.toFixed(2) + '%'); } SEE DEMO With hours, minutes and seconds: var seconds = 1000; var minutes = seconds * 60; var hours = minutes * 60; var days = hours * 24; var years = days * 365; function updateProgress(percentage) { var x = (percentage/timeTotal)*100, y = x.toFixed(3); $('#pbar_innerdiv').css("width", x + "%"); $('#pbar_innertext').css("left", x + "%").text( Math.floor(percentage/hours) + 'h ' + Math.floor(percentage/minutes) + 'm ' + Math.floor(percentage/seconds) + 's'); } SEE DEMO. I think for that you should use requestAnimationFrame: var timeTotal = 2500, timeStart = 0, cFlag = false; var seconds = 1000; var minutes = seconds * 60; var hours = minutes * 60; var days = hours * 24; var years = days * 365; function updateProgress() { var time = new Date().getTime() - timeStart; var x = (time/timeTotal)*100, y = x.toFixed(3); $('#pbar_innerdiv').css("width", x + "%"); $('#pbar_innertext').css("left", x + "%").text( Math.floor(time/hours) + 'h ' + Math.floor(time/minutes) + 'm ' + Math.floor(time/seconds) + 's'); if(time <= timeTotal && cFlag) { requestAnimationFrame(updateProgress); } } $(document).ready(function() { $('#pbar_outerdiv').click(function() { if (cFlag === false) { cFlag = true; timeStart = new Date().getTime(); updateProgress(); } else { cFlag = false; } }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="pbar_outerdiv" style="width: 300px; height: 20px; border: 1px solid grey; z-index: 1; position: relative; border-radius: 5px; -moz-border-radius: 5px;"> <div id="pbar_innerdiv" style="background-color: lightblue; z-index: 2; height: 100%; width: 0%;"></div> <div id="pbar_innertext" style="z-index: 3; position: absolute; top: 0; left: 0; height: 100%; color: black; font-weight: bold; text-align: center;">0h 0m 0s</div> </div> <p>Click once to start!</p> <p>Click again to toggle Start/Stop</p> UPDATED WITH PAUSE ALLOW AND RESUME: var timeTotal = 5 * minutes, timePaused = 0, timePausedStart = 0, timeStart = 0, cFlag = false, stopped = false; var seconds = 1000; var minutes = seconds * 60; var hours = minutes * 60; var days = hours * 24; var years = days * 365; function updateProgress() { if( !timePausedStart ) { // if not paused var time = new Date().getTime() - timeStart - timePaused; var x = (time/timeTotal)*100, y = x.toFixed(3); $('#pbar_innerdiv').css("width", x + "%"); $('#pbar_innertext').css("left", x + "%").text( Math.floor(time/hours%24) + 'h ' + Math.floor(time/minutes%60) + 'm ' + Math.floor(time/seconds) + 's'); if(time > timeTotal) { cFlag = false; } if( Math.floor(time/seconds) == 3*60+30 && !stopped){ // pause at 3 m 30 s stopped = true; pause(); } } if( cFlag ) requestAnimationFrame(updateProgress); } $(document).ready(function() { $('#pbar_outerdiv').click(function() { if (cFlag === false) { cFlag = true; timeStart = new Date().getTime(); timePaused = 0; updateProgress(); } else if( cFlag === true && timePausedStart ){ // reset pause timePaused += new Date().getTime() - timePausedStart; timePausedStart = 0; } else { pause(); } }); }); var pause = function(){ timePausedStart = new Date().getTime(); }; #pbar_outerdiv { cursor: pointer; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="pbar_outerdiv" style="width: 300px; height: 20px; border: 1px solid grey; z-index: 1; position: relative; border-radius: 5px; -moz-border-radius: 5px;"> <div id="pbar_innerdiv" style="background-color: lightblue; z-index: 2; height: 100%; width: 0%;"></div> <div id="pbar_innertext" style="z-index: 3; position: absolute; top: 0; left: 0; height: 100%; color: black; font-weight: bold; text-align: center;">0h 0m 0s</div> </div> <p>Click once to start!</p> <p>Click again to toggle Start/Stop</p>
Instead of printing (percentage / 1000).toFixed(2), you can use percentage to split your time in millisecond to h:m's.ms You can simply compute the desired values with integer divisions h = Math.floor(percentage / 3600000); m = Math.floor((percentage - h * 3600000) / 60000); s = Math.floor((percentage - h * 3600000 - m * 60000) / 1000); ms = Math.floor(percentage - h * 3600000 - m * 60000 - s * 1000); Then, you can just use toString() to convert your int to strings. I concatenated the values with 0 and and used slice() to keep only the two last character. This allow you to print hours, minutes, ... in two digits format text = ("0" + h.toString() ).slice(-2) + ":" + ("0" + m.toString() ).slice(-2) + "'" + ("0" + s.toString() ).slice(-2) + "\"" + ("0" + ms.toString()).slice(-2);