-
Notifications
You must be signed in to change notification settings - Fork 9
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
base: master
Are you sure you want to change the base?
Conversation
WalkthroughThe countdown script in Changes
Poem
TipsChat with CodeRabbit Bot (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assets/js/count.js
Outdated
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){ | ||
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); | ||
} | ||
}, second) |
There was a problem hiding this comment.
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 a conditional check to ensure that the countdown does not display negative values. This should resolve the issue with JavaScript console errors related to past dates. However, it would be beneficial to also handle the scenario when the countdown reaches zero. Currently, the code does not specify what should happen once the countdown is finished. Consider updating the display to show a message or hide the countdown elements when the target date and time are reached.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the code has issue as it does not deal with the case when an html element is not found and we still call its innerText
method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gitcommitshow You're absolutely right. The current implementation assumes that the HTML elements with the specified IDs will always be present. To make the code more robust, we should add checks to ensure that these elements exist before attempting to update their innerText
. Here's how we can modify the code to include these checks:
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);
}
This way, we only attempt to update the innerText
of an element if it has been successfully retrieved from the DOM. Thank you for pointing out this improvement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gitcommitshow Thank you for the review. If there are any specific issues or further improvements you would like to suggest, please let me know, and I'll address them accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
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.
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) |
Summary by CodeRabbit
New Features
Bug Fixes