Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: count js console error #181

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions assets/js/count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ minute = second * 60,
hour = minute * 60,
day = hour * 24;

var countDown = new Date('Nov 27, 2021 10:00:00').getTime(),
var countDown = new Date('Jan 27, 2024 10:00:00').getTime(),
x = setInterval(function() {

var now = new Date().getTime(),
distance = countDown - now;

document.getElementById('days').innerText = Math.floor(distance / (day)),
document.getElementById('hours').innerText = Math.floor((distance % (day)) / (hour)),
document.getElementById('minutes').innerText = Math.floor((distance % (hour)) / (minute)),
document.getElementById('seconds').innerText = Math.floor((distance % (minute)) / second);

var now = new Date().getTime(),
distance = countDown - now;
if(distance > 0){
var daysElement = document.getElementById('days'),
hoursElement = document.getElementById('hours'),
minutesElement = document.getElementById('minutes'),
secondsElement = document.getElementById('seconds');

if(daysElement) daysElement.innerText = Math.floor(distance / (day));
if(hoursElement) hoursElement.innerText = Math.floor((distance % (day)) / (hour));
if(minutesElement) minutesElement.innerText = Math.floor((distance % (hour)) / (minute));
if(secondsElement) secondsElement.innerText = Math.floor((distance % (minute)) / second);
}
}, second)
Comment on lines +7 to 22
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes correctly update the countdown target date and add conditional logic to prevent negative values from being displayed. The script now checks for the existence of countdown elements before attempting to update them, which is a good practice to avoid errors when elements are missing in the DOM. However, the variable distance is not declared with var, let, or const, which can lead to it being implicitly declared as a global variable, potentially causing conflicts or bugs in the global scope.

-  distance = countDown - now;
+  var distance = countDown - now;

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
var countDown = new Date('Jan 27, 2024 10:00:00').getTime(),
x = setInterval(function() {
var now = new Date().getTime(),
distance = countDown - now;
document.getElementById('days').innerText = Math.floor(distance / (day)),
document.getElementById('hours').innerText = Math.floor((distance % (day)) / (hour)),
document.getElementById('minutes').innerText = Math.floor((distance % (hour)) / (minute)),
document.getElementById('seconds').innerText = Math.floor((distance % (minute)) / second);
var now = new Date().getTime(),
distance = countDown - now;
if(distance > 0){
var daysElement = document.getElementById('days'),
hoursElement = document.getElementById('hours'),
minutesElement = document.getElementById('minutes'),
secondsElement = document.getElementById('seconds');
if(daysElement) daysElement.innerText = Math.floor(distance / (day));
if(hoursElement) hoursElement.innerText = Math.floor((distance % (day)) / (hour));
if(minutesElement) minutesElement.innerText = Math.floor((distance % (hour)) / (minute));
if(secondsElement) secondsElement.innerText = Math.floor((distance % (minute)) / second);
}
}, second)
var countDown = new Date('Jan 27, 2024 10:00:00').getTime(),
x = setInterval(function() {
var now = new Date().getTime(),
var distance = countDown - now;
if(distance > 0){
var daysElement = document.getElementById('days'),
hoursElement = document.getElementById('hours'),
minutesElement = document.getElementById('minutes'),
secondsElement = document.getElementById('seconds');
if(daysElement) daysElement.innerText = Math.floor(distance / (day));
if(hoursElement) hoursElement.innerText = Math.floor((distance % (day)) / (hour));
if(minutesElement) minutesElement.innerText = Math.floor((distance % (hour)) / (minute));
if(secondsElement) secondsElement.innerText = Math.floor((distance % (minute)) / second);
}
}, second)