How to Restart Countdown Timer After Every 90 Hours - javascript

$(document).ready(function()
{
var days = 03; var hours = 18; var minutes = 00; var seconds = 00;
function calculate()
{
setTimeout(calculate, 1000); $('#showDate').html(days + ':' + hours + ':' + minutes + ':' + seconds);
seconds--;
if (seconds < 0) { seconds = 59; minutes--;
if (minutes < 0) { hours--; minutes = 59;
if (hours < 0) { days--; hours = 23;
if (days < 0) { days = 0; hours = 0; minutes = 0; seconds = 0;
}}}}
}
calculate();
});
<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
<div id='showDate'></div>
Hope so you all will be fine.
Come to the Point, I want to reset the count down that i created using jquery after every 90 hours.
I had written below code yet.
It does not reset after 90 hours means after 3 days and 18 hours.
Kindly Help me.
<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
<script type='text/javascript'>
$(document).ready(function()
{
var days = 03; var hours = 18; var minutes = 00; var seconds = 00;
function calculate()
{
setTimeout(calculate, 1000); $('#showDate').html(days + ':' + hours + ':' + minutes + ':' + seconds);
seconds--;
if (seconds < 0) { seconds = 59; minutes--;
if (minutes < 0) { hours--; minutes = 59;
if (hours < 0) { days--; hours = 23;
if (days < 0) { days = 0; hours = 0; minutes = 0; seconds = 0;
}}}}
}
calculate();
});
</script>
<div id='showDate'></div>

You can try something like following :
<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
<script type='text/javascript'>
$(document).ready(function()
{
var days = 00; var hours = 00; var minutes = 1; var seconds = 00;
function calculate()
{
setTimeout(calculate, 1000); $('#showDate').html(days + ':' + hours + ':' + minutes + ':' + seconds);
if(days == 0 && hours ==00 && minutes == 0 && seconds == 00)
{
minutes = 1;
}
seconds--;
if (seconds < 0) { seconds = 59; minutes--;
if (minutes < 0) { hours--; minutes = 59;
if (hours < 0) { days--; hours = 23;
if (days < 0) { days = 0; hours = 0; minutes = 0; seconds = 0;
}}}}
}
calculate();
});
</script>
<div id='showDate'></div>

<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
<script type='text/javascript'>
$(document).ready(function()
{
var days = 03; var hours = 18; var minutes = 00; var seconds = 00;
function calculate()
{
setTimeout(calculate, 1000); $('#showDate').html(days + ':' + hours + ':' + minutes + ':' + seconds);
seconds--;
if (seconds < 0) { seconds = 59; minutes--;
if (minutes < 0) { hours--; minutes = 59;
if (hours < 0) { days--; hours = 23;
if (days < 0) { days = 0; hours = 0; minutes = 0; seconds = 0;
}}}}
if((seconds == 0) && (minutes == 0) && (hours == 0) && (days == 0))
{
days = 03;
hours = 18;
minutes = 00;
seconds = 20;
}
}
calculate();
});
</script>
<div id='showDate'></div>

Related

How to replace "0:00" with "Collect" in timer button

In the last days I had asked a question how to make a button that is disabled for 1 minute and when it is clickable again +25 points are added to a div. The problem is: When the timer is over it says "0:00". Is there a way to replace the "0:00" with "Collect"? I found similar questions on stackoverflow but they didn't help me.
Here is my code:
$('#btn').prop('disabled',true);
startCountDown();
function getCounter(){
return parseInt($('#counter').html());
}
function setCounter(count) {
$('#counter').html(count);
}
$("#btn").click(function() {
setCounter(getCounter()+25);
$('#btn').prop('disabled',true);
startCountDown();
});
function startCountDown() {
var minutes = 0,
seconds = 59;
$("#countdown").html(minutes + ":" + seconds);
var count = setInterval(function() {
if (parseInt(minutes) < 0 || parseInt(seconds) <=0 ) {
$("#countdown").html(minutes + ":" + seconds);
clearInterval(count);
$('#btn').prop('disabled',false);
} else {
$("#countdown").html(minutes + ":" + seconds);
seconds--;
if (seconds < 10) seconds = "0" + seconds;
}
}, 1000);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="counter">0</div>
<button id="btn">
<span id="countdown">0:00</span>
</button>
UPDATE:
I have updated my code as you can see in the second snippet. Unfortunately, I now have the problem that the number 0:01 has been replaced with "Collect". (So: 0:03, 0:02, Collect(disabled), Collect(enabled)). Here is the code:
$('#btn').prop('disabled',true);
startCountDown();
function getCounter(){
return parseInt($('#counter').html());
}
function setCounter(count) {
$('#counter').html(count);
}
$("#btn").click(function() {
setCounter(getCounter()+25);
$('#btn').prop('disabled',true);
startCountDown();
});
function startCountDown() {
var minutes = 0,
seconds = 60;
$("#countdown").html(minutes + ":" + seconds);
var count = setInterval(function() {
if (parseInt(minutes) < 0 || parseInt(seconds) <=0 ) {
$("#countdown").html(minutes + ":" + seconds);
clearInterval(count);
$('#btn').prop('disabled',false);
} else {
$("#countdown").html(minutes + ":" + seconds);
seconds--;
if (seconds < 10) seconds = "0" + seconds;}
if (seconds == 0) {// replacing 0:00 with "Collect" is right here
$('#countdown').html("Collect");
}
}, 1000);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="counter">0</div>
<button id="btn">
<span id="countdown">Collect</span>
</button>
Hello GuciiBananaKing99,
I have found the solution to your problem. You need to add an if statement when seconds is 0. Then change $('#btn').html to "Collect".
Here is the full code:
function startCountDown() {
var minutes = 0,
seconds = 59;
$("#countdown").html(minutes + ":" + seconds);
var count = setInterval(function() {
if (parseInt(minutes) < 0 || parseInt(seconds) <=0 ) {
$("#countdown").html(minutes + ":" + seconds);
clearInterval(count);
$('#btn').prop('enabled',false);
} else {
$("#countdown").html(minutes + ":" + seconds);
seconds--;
if (seconds < 10) {seconds = "0" + seconds;}
if (seconds == 0) { // Check if seconds is 0
$('#btn').html("Collect"); // Change Btn's HTML to Collect
});
}
}, 1000);
}

How to make button displayed from 7:30am-9:30am everyday

I know how to make the button appear if the time is hourly:
if ((hr == 7) || (hr == 8) || (hr == 9)) {
displaybutton.style.visibility = "visible";
}
But I'm not sure how to do it if I want to set it up at 7:30am to 9:30am instead of 7am to 9am.
window.onload = function(){
DisplayCurrentTime();
};
function DisplayCurrentTime() {
var date = new Date();
var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
var ampm = (hours >= 12) ? "PM" : "AM";
time = hours + ":" + minutes + ":" + seconds;
if(hours>=7 && hours<10 && ampm=="AM")
{
if(minutes<=30)
{
var lblTime = document.getElementById("time");
lblTime.innerHTML = time;
$("#idbuttontoshow").css({"display":"block"});
}
}
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="two"></div>
<br>
<div id="time"></div>
<div id="idbuttontoshow" style="display:none;">
<button>one</button>
<button>two</button>
<button>three</button>
</div>

Countdown days, hours, minutes, seconds-made script jquery

How can I have a countdown timer with predetermined time?
My code
<html>
<head>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
<script type='text/javascript'>
$(document).ready(function() {
var seconds = 6;
var minutes = 1;
function calculate() {
setTimeout(calculate, 1000);
$('#showDate').html(' expires after ' + minutes + ' minutes ' + seconds + ' seconds ');
seconds--;
if (seconds < 0) {
seconds = 59;
minutes--;
if (minutes < 0) {
minutes = 0;
seconds = 0;
}
}
}
calculate();
});
</script>
</head>
<body>
<div id='showDate'></div>
</body>
</html>
The above code applies only to minutes and seconds. How can I add hours and days?
Edited based on the method that was already being used.
<html>
<head>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
<script type='text/javascript'>
$(document).ready(function() {
var days = 4;
var hours = 0;
var minutes = 0;
var seconds = 6;
function calculate() {
setTimeout(calculate, 1000);
$('#showDate').html(' expires after ' + days + ' days ' + hours + ' hours ' + minutes + ' minutes ' + seconds + ' seconds ');
seconds--;
if (seconds < 0) {
seconds = 59;
minutes--;
if (minutes < 0) {
hours--;
minutes = 59;
if (hours < 0) {
days--;
hours = 23;
if (days < 0) {
days = 0;
hours = 0;
minutes = 0;
seconds = 0;
}
}
}
}
}
calculate();
});
</script>
</head>
<body>
<div id='showDate'></div>
</body>
</html>
<html>
<head>
</head>
<body>
<div id="clockdiv">
expires after <span class="days"></span> day <span class="hours"></span> hours <span class="minutes"></span> minutes <span class="seconds">0</span> seconds
<script type="text/javascript">
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()) + 15 * 24 * 60 * 60 * 1000);
initializeClock('clockdiv', deadline);
</script>
</body>
</html>

Count Down Timer shows NaN

I'm having a problem with a count down timer made in JavaScript. It was working for the last 2 weeks, but today it started to show NaN:NaN... , and I can't understand why. Here is the code, does anyone have any idea which one could be the problem?
<div id="countdownmain">
<span id="countdownmain" class="timer"></span>
</div>
<script>
var date = new Date;
var secondsnow = date.getSeconds();
var minutesnow = date.getMinutes();
var hournow = date.getHours();
var day = date.getDay();
var passatti = (secondsnow + (minutesnow*60) + (hournow*3600));
if((day==1)||(day==2)||(day==3)||(day==4)){
if(passatti < 46800){
var upgradeTime = 46800 - passatti;
}else if(passatti > 46800){
var upgradeTime = 86400 - passatti + 46800;
}
}else if((day==5)&&(passatti < 46800)){
var upgradeTime = 46800 - passatti;
}else if((day==5)&&(passatti > 46800)){
var upgradeTime = 86400 - passatti + 46800 + 86400 + 86400;
}else if((day==6)){
var upgradeTime = 86400 - passatti + 46800 + 86400;
}else if((day==7)){
var upgradeTime = 86400 - passatti + 46800;
}
var seconds = upgradeTime;
function timer() {
var now = Math.floor(Date.now() / 1000);
('0' + 11).slice(-2)
var days = Math.floor(seconds/24/60/60);
var hoursLeft = Math.floor((seconds) - (days*86400));
var hours = Math.floor(hoursLeft/3600);
var minutesLeft = Math.floor((hoursLeft) - (hours*3600));
var minutes = Math.floor(minutesLeft/60);
var remainingSeconds = seconds % 60;
if (remainingSeconds < 10) {
remainingSeconds = "0" + remainingSeconds;
}
document.getElementById('countdownmain').innerHTML = "<span class='timesm'> For same working day dispatch, order in </span><br class='appear'><span style='display:inline-block; width:45px;'><span class='hideDays glowW'>" + ('0' + days).slice(-2) + "</span></span><span class='times'> days </span><span style='display:inline-block; width:45px;'><span class='hideHours glowW'>" + ('0' + hours).slice(-2) + "</span></span><span class='times'> hours </span><span style='display:inline-block; width:45px;'><span class='hideMinutes glowW'>" + ('0' + minutes).slice(-2) + "</span></span><span class='times'> minutes </span><span style='display:inline-block; width:45px;'><span class='hideSec glowW'>" + ('0' + remainingSeconds).slice(-2) + "</span></span><span class='times'> seconds </span>";
if (seconds == 0) {
clearInterval(countdownTimer);
//document.getElementById('countdownmain').innerHTML = "Completed";
seconds = upgradeTime;
} else {
seconds--;
$('.hideSec').fadeOut('slow');
}
if(('0' + remainingSeconds).slice(-2)==00){
$('.hideMinutes').fadeOut('slow');
}
if((('0' + minutes).slice(-2)==00)&&(('0' + remainingSeconds).slice(-2)==00)){
$('.hideHours').fadeOut('slow');
}
if((('0' + hours).slice(-2)==00)&&(('0' + minutes).slice(-2)==00)&&(('0' + remainingSeconds).slice(-2)==00)){
$('.hideDays').fadeOut('slow');
}
}
var countdownTimer = setInterval('timer()', 1000);
</script>
Unfortunately, today is Sunday! Javascript return 0 value as first of a week in getDay().
Take a look my fiddle. I just decreased day comparator value in if statement. (e.g. 1 -> 0, 2 -> 1, 3 -> 2 and so on..)

JS Code Works in jsbin and Not in jsfiddle or Chrome/Safari?

I am trying to figure out why this code is working only in jsbin and not in jsfiddle or in any web browser as an html/js file. I have tried debugging but cannot find a conclusion.
I made the mistake of coding directly in jsbin instead of a document. Any input would be appreciated.
http://jsbin.com/tuduxedohe/7/edit
http://jsfiddle.net/2rs1x5pz/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Timer</title>
<script type="text/javascript" src="part2.js"></script>
</head>
<body>
<h1><div id="time">00:00:00</div></h1>
<div id="result"></div>
<button id="start" onclick ="startClock();" >Start</button>
<button id="stop" onclick="stopTimer();">Stop</button>
<button id="clear" onclick="resetTimer();">Reset</button>
</body>
</html>
var currentTime = document.getElementById('time');
var hundreths = 0;
var seconds = 0;
var minutes = 0;
var t;
function startClock() {
function add() {
hundreths++;
if (hundreths > 99) {
hundreths = 0;
seconds++;
if (seconds > 59) {
seconds = 0;
minutes++;
}
if(minutes >= 10) {
seconds= 0;
minutes= 0;
stopTimer();
}
}
if (hundreths > 9 && seconds < 9) {
currentTime.innerHTML = "0" + minutes + ":" + "0" + seconds + ":" + hundreths;
}
else if ((seconds > 9 ) && (hundreths < 9)) {
currentTime.innerHTML = "0" + minutes + ":" + seconds + ":" + "0" + hundreths;
}
else if((seconds > 9) && (hundreths > 9)) {
currentTime.innerHTML = "0" + minutes + ":" + seconds + ":" + hundreths;
}
else if ((minutes > 9) && (seconds < 9) && (hundreths < 9)) {
currentTime.innerHTML = minutes + ":" + "0" + seconds + ":" + "0" + hundreths;
}
else if ((minutes > 9) && (seconds > 9) && (hundreths < 9)) {
currentTime.innerHTML = minutes + ":" + seconds + ":" + "0" + hundreths;
}
else if ((minutes > 9) && (seconds > 9) && (hundreths < 9)) {
currentTime.innerHTML = minutes + ":" + seconds + ":" + hundreths;
}
else {
currentTime.innerHTML = "0" + minutes + ":" + "0" + seconds + ":" + "0" + hundreths;
}
timer();
}
function timer() {
t = setTimeout(add, 1);
}
timer();
} // end function start clock
function stopTimer() {
document.getElementById("result").innerHTML = "<p>" + ("Your time is: " + minutes + " minutes, " + seconds + " seconds, " + "and " + hundreths + " hundreths") + "</p>";
clearTimeout(t);
}
function resetTimer() {
hundreths = 0;
seconds = 0;
minutes = 0;
currentTime.innerHTML = "00:00:00";
}
That is because by default the script is added in a onload handler in jsfiddle, so your methods is available only inside the scope of that closure. So it will be
window.onload=function(){
//your script is here
}
You are trying to access them in global scope when you are trying to call them from on<event>="" attributes which will give an error like Uncaught ReferenceError: startClock is not defined in your console.
Change the second dropdown in the left panel under Frameworks & Extensions to body/head in the left panel of fiddle to add the script without a wrapper function
Demo: Fiddle

Categories