Javascript countdown won't work - javascript

I am trying to make a Javascript countdown for my website, so as a start I wanted to test the best solution. I now have a code, but when I open the website nothing is on there, I think I might have done something wrong when trying to implement Javascript into html?
My code:
<!DOCTYPE html>
<html>
<head>
<title>Home Test Countdown</title>
<script LANGUAGE="Javascript">
var target_date = new Date("Aug 15, 2019").getTime();
var days, hours, minutes, seconds;
var countdown = document.getElementById("countdown");
setInterval(function () {
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;
hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;
minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);
countdown.innerHTML = days + "d, " + hours + "h, "
+ minutes + "m, " + seconds + "s";
}, 1000);
</script>
</head>
<body>
<span id="countdown"></span>
</body>
</html>

Since you have the script in the head, when var countdown = document.getElementById("countdown"); is executed the element is not present in the dom so countdown will be null which will result in the error Uncaught TypeError: Cannot set property 'innerHTML' of null in line countdown.innerHTML = '...'.
One possible solution is to move your code to a window load event handler
window.addEventListener('load', function () {
var target_date = new Date("Aug 15, 2019").getTime();
var days, hours, minutes, seconds;
var countdown = document.getElementById("countdown");
setInterval(function () {
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;
hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;
minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);
countdown.innerHTML = days + "d, " + hours + "h, " + minutes + "m, " + seconds + "s";
}, 1000);
})
Demo: Fiddle
Another is to move your script to the bottom of the page after <span id="countdown"></span>

This code is working you need to add your code after your HTML elements(Add the javascript code before closing the body tag)
or try to add your code in window.onload like,
window.onload = function(){
// your timer code goes here
};
var target_date = new Date("Aug 15, 2019").getTime();
var days, hours, minutes, seconds;
var countdown = document.getElementById("countdown");
setInterval(function() {
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;
hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;
minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);
countdown.innerHTML = days + "d, " + hours + "h, " + minutes + "m, " + seconds + "s";
}, 1000);
<span id="countdown"></span>
Also you added the tag jquery so you can add your code in $.ready() like,
$(function(){
// your code here
});

The script needs to be after the span countdown object

Related

countdownt to go website on codeigniter

I tried to make my first countdown for the web , how to create a script of the countdown after the time runs straight into my web ?? in codeIgniter
this my script
var target_date = new Date('Sep, 19, 2015,21:26:00').getTime();
var days, hours, minutes, seconds;
var countdown = document.getElementById('countdown');
setInterval(function () {
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;
hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;
minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);
countdown.innerHTML = '<div id="days" class="timer_box"><h1>' + days + '</h1> <p>Days</p></div> <div id="days" class="timer_box"><h1>' + hours + '</h1> <p>Hours</p></div> <div id="days" class="timer_box"><h1>'
+ minutes + '</h1> <p>Minutes</p></div> <div id="days" class="timer_box"><h1>' + seconds + '</h1> <p>Seconds</p></div>';
}, 1000);

Countdown reset on specific time

Currently I have a script, which has a countdown for a specific date, but I want the countdown to be specific on a timer, so let's say if I start the timer and I have set the timer to run for 30 days, it will then run for 30 days and then reset back to 30 days again and start running. Is it possible to change it to do so?
My code:
<body>
<span id="countdown"></span>
<script LANGUAGE="Javascript">
// set the date we're counting down to
var target_date = new Date("Apr 9, 2015").getTime();
// variables for time units
var days, hours, minutes, seconds;
// get tag element
var countdown = document.getElementById("countdown");
// update the tag with id "countdown" every 1 second
setInterval(function () {
// find the amount of "seconds" between now and target
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;
// do some time calculations
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;
hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;
minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);
// format countdown string + set tag value
countdown.innerHTML = days + "d, " + hours + "h, "
+ minutes + "m, " + seconds + "s";
}, 1000);
</script>
</body>
EDIT:
I have now changed the code to look like underneath, but now when I open the website in my browser its blank.
New code:
<span id="countdown"></span>
<script LANGUAGE="Javascript">
// set the date we're counting down to
var target_date = new Date("Apr 9, 2015").getTime();
// variables for time units
var days, hours, minutes, seconds;
// get tag element
var countdown = document.getElementById("countdown");
if (seconds_left <= 0){
target_date = target_date + 30 days;
}
// update the tag with id "countdown" every 1 second
setInterval(function () {
// find the amount of "seconds" between now and target
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;
// do some time calculations
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;
hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;
minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);
// format countdown string + set tag value
countdown.innerHTML = days + "d, " + hours + "h, "
+ minutes + "m, " + seconds + "s";
}, 1000);
I really advice you to take advantage of JavaScript libraries , in your case moment JS is the perfect solution, you can check their documentation and see how you can manage time easily. Anyways here is the solution of your question using moment js.
First download moment js and add it to your page.
HTML CODE
<span id="days"> </span>
<span id="hours"></span>
<span id="minutes"></span>
<span id="seconds"></span>
It can not get simple than that :)
JAVASCRIPT
//create two variables for holding the date for 30 back from now using substract
var back30Days = moment().subtract(30, 'days').format('YYYY-MM-DD H:mm:ss');
var countDownSeconds = Math.floor(moment().diff(back30Days, 'seconds'));
//variables holding days, hours , minutes and seconds
var Days, Minutes, Hours, Seconds;
// Set Interval function for performing all calculations and decrementing the countDownSeconds
setInterval(function () {
// Updating Days
Days = pad(Math.floor(countDownSeconds / 86400), 2);
// Updating Hours
Hours = pad(Math.floor((countDownSeconds - (Days * 86400)) / 3600), 2);
// Updating Minutes
Minutes = pad(Math.floor((countDownSeconds - (Days * 86400) - (Hours * 3600)) / 60), 2);
// Updating Seconds
Seconds = pad(Math.floor((countDownSeconds - (Days * 86400) - (Hours * 3600) - (Minutes * 60))), 2);
// Updation our HTML view
document.getElementById("days").innerHTML = Days + ' Days';
document.getElementById("hours").innerHTML = Hours + ' Hours';
document.getElementById("minutes").innerHTML = Minutes + ' Minutes';
document.getElementById("seconds").innerHTML = Seconds + ' Seconds';
// Decrement the countDownSeconds
countDownSeconds--;
// If we reach zero , our chrono should reset to 30 days back again, as you told
if (countDownSeconds === 0) {
countDownSeconds = Math.floor(moment().diff(back30Days, 'seconds'));
}
}, 1000);
// Function for padding the seconds i.e limit it only to 2 digits
function pad(num, size) {
var s = num + "";
while (s.length < size) s = "0" + s;
return s;
}
Here is a jsfiddle

Looping foreach, grabbing data from db and insert into JS

I have some JS that display a countdown timer, but this is only showing for one item on the page and i need it to be used in the PHP foreach loop I have. this code below is within the foreach loop.
<div class="countdown-timer">
<strong>Time Left - </strong> <span id="countdown"></span>
</div>
<script type="text/javascript" charset="utf-8">
// set the date we're counting down to
var target_date = new Date("<?php echo $listing->ends; ?>").getTime(); // echo would return 2014-07-15
// variables for time units
var days, hours, minutes, seconds;
// get tag element
var countdown = document.getElementById("countdown");
// update the tag with id "countdown" every 1 second
setInterval(function () {
// find the amount of "seconds" between now and target
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;
// do some time calculations
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;
hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;
minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);
// format countdown string + set tag value
countdown.innerHTML = days + " days " + hours + " hrs "
+ minutes + " min " + seconds + " secs ";
}, 1000);
</script>
You'll need to create a unique ID for each element, that way you can reference each element individually.
In addition, you'd be wise to take the javascript block out of the php foreach loop. Instead, create an array of each element's ID, then use that array to setInterval on each item via javascript. That way, you don't repeat a bunch of JS code.
Rough example:
<!-- php code here to loop and create these html elements -->
<div class="countdown-timer">
<strong> Time Left - </strong> <span id="countdown-INSERT_ID_FROM_PHP_HERE"></span>
</div>
<!--end php code to create html -->
<script type="text/javascript">
var createCountdown = function(id, date) {
var target_date = new Date(date).getTime();
var days, hours, minutes, seconds;
var countdown = document.getElementById(id);
setInterval(function() {
var current_date = new Date().getTIme();
var seconds_left = (target_date - current_date) / 1000;
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;
hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;
minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);
// format countdown string + set tag value
countdown.innerHTML = days + " days " + hours + " hrs "
+ minutes + " min " + seconds + " secs ";
}, 1000);
};
// USE PHP TO MAKE THIS ARRAY
// POPULATE IT WITH OBJECTS THAT LOOK LIKE THIS
// { id: "countdown-6", date: "2014-07-01" }
var allCounters = [
{ id: "countdown-6", date: "2014-07-01" }
];
allCounters.forEach(function(counter) {
createCountdown(counter.id, counter.date);
});
</script>

How To add multiple Countdown timer query in a same html page?

I want to create a online auction site. I need to mention every auction ending time & date.
For this I have created a countdown.js file like this:
// set the date we're counting down to
var target_date = new Date("Aug 15, 2014").getTime();
// variables for time units
var days, hours, minutes, seconds;
// get tag element
var countdown = document.getElementById("countdown");
// update the tag with id "countdown" every 1 second
setInterval(function () {
// find the amount of "seconds" between now and target
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;
// do some time calculations
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;
hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;
minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);
// format countdown string + set tag value
countdown.innerHTML = days + " D : " + hours + " H : " + minutes + " M : " + seconds + " S";
}, 1000);
And I have imported this in my html as follows:
<script type="text/javascript"src="js/countdown.js"></script>
<p style="font-size:larger;color:Red;font-weight:bolder">END IN</p>
<h5><i id="countdown"></i></h5><br />
It's working fine. Now I want to add one or more timer in the same page. While I use this code
<h5><i id="countdown"></i></h5><br />
It's showing an error in ID. How to rectify this?
Just move your code into one function which is accepting 2 params elementId and target date
function setTimer(elem_id, date) {
// set the date we're counting down to
var target_date = new Date(date).getTime();
// variables for time units
var days, hours, minutes, seconds;
// get tag element
var countDownElem = document.getElementById(elem_id);
//update the tag with id "countdown" every 1 second
setInterval(function () {
// find the amount of "seconds" between now and target
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;
// do some time calculations
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;
hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;
minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);
// format countdown string + set tag value
countDownElem.innerHTML = days + " D : " + hours + " H : " + minutes + " M : " + seconds + " S";
}, 1000);
}
setTimer("countdown1","Aug 15, 2014");
setTimer("countdown2","Aug 17, 2014");
Here is the fiddle for above code
The answer above works, but I noticed the second timer is showing to be an hour longer away than the first timer.

My timer is restarting when ever i press back button or reload button. How to avoid this?

Hi guys i am using a timer for 20 mins in my code.(Its working)
But when ever i press back button or reload button its restarting.
how to avoid this ?
any one got code to disable back button in browser ?
Please any one can help me with this.
<html>
<head>
<script language="javascript">
function timedText()
{
var x=document.getElementById('txt');
var wc=setTimeout(function(){document.getElementById("txt").disabled=true;},100);
var tu=setTimeout(function(){x.value="20 min "},100);
var flag=1200000;
var to = setInterval(function(){if(flag > 0)
{
x.value= flag/60000 + " min";
flag-=60000;
}
else
{
document.getElementById("submit_id").click();
clearInterval(to);
}
},60000);
}
</script>
</head>
<body onload="timedText()">
<form action="">
<p align='right'><b>Time left </b><input type="text" id="txt" /></p>
</form>
</body>
</html>
If i onderstand what you want is a countdown with seconds:
// set the date we're counting down to
var target_date = new Date("Aug 15, 2019").getTime();
// variables for time units
var days, hours, minutes, seconds;
// get tag element
var countdown = document.getElementById("countdown");
// update the tag with id "countdown" every 1 second
setInterval(function () {
// find the amount of "seconds" between now and target
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;
// do some time calculations
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;
hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;
minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);
// format countdown string + set tag value
countdown.innerHTML = days + "d, " + hours + "h, "
+ minutes + "m, " + seconds + "s";
}, 1000);
And in html:
<span id="countdown"></span>
I hope this helped.

Categories