0

I have made a JavaScript counter like this:

window.onload = 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); 

HTML:

<span id="countdown"></span>

The browser (Google Chorme) says:

Uncaught ReferenceError: target_date is not defined

Even if I remove the window.onload = function(){}, the will still not work. What have I done wrong?

Bob__
12.9k3 gold badges30 silver badges44 bronze badges
asked Oct 21, 2013 at 7:53
2
  • declare target_date out of the window.onload scope Commented Oct 21, 2013 at 7:56
  • When I removed the window.onload it worked fine - jsfiddle.net/dangoodspeed/6Xj5c Commented Oct 21, 2013 at 7:58

3 Answers 3

3

It's all to do with variable scope. Here your target_date is defined within your window.onload, making it local to that function. If you want to use the variable outside of that function, declare it globally by moving it outside of the function:

var target_date; /* Declared globally. */
window.onload = function(){
 target_date = new Date("Aug, 15, 2019").getTime();
}
answered Oct 21, 2013 at 7:56
Sign up to request clarification or add additional context in comments.

Comments

0

Change your first "}" to the end of the script

window.onload = 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); 
 }
answered Oct 21, 2013 at 8:01

Comments

0

Just move the curly bracket "}" of the window.onload to the end of the script

window.onload = 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);
}
answered Oct 21, 2013 at 8:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.