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

Conversation

gitcommitshow
Copy link
Member

@gitcommitshow gitcommitshow commented Nov 20, 2023

Summary by CodeRabbit

  • New Features

    • Updated the countdown feature to reflect the new target date of January 27, 2024.
  • Bug Fixes

    • Implemented checks to ensure countdown does not display negative values.
    • Enhanced countdown robustness by conditionally updating elements only when necessary.

Copy link

coderabbitai bot commented Nov 20, 2023

Walkthrough

The countdown script in count.js has been updated to ensure that the countdown timer reflects a new target date and no longer shows negative values once the countdown has reached zero. The script now includes conditional logic to update the display only when the countdown is positive, enhancing the user experience by preventing the display of confusing negative time values.

Changes

File Path Change Summary
assets/js/count.js Updated countdown target date and added conditions to prevent negative values in the countdown display.

Poem

As winter whispers, the clock ticks anew,
No more negatives, just a future in view.
Code hops along, with a timely fix,
A countdown to joy, no more negative ticks. 🐇⏲️✨


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between d8f34d1 and 1a835c1.
Files selected for processing (1)
  • assets/js/count.js (1 hunks)

Comment on lines 7 to 17
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)
Copy link

@coderabbitai coderabbitai bot Nov 20, 2023

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.

Copy link
Member Author

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

Copy link

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

@coderabbitai review

Copy link

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.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 1a835c1 and f636963.
Files selected for processing (1)
  • assets/js/count.js (1 hunks)

Comment on lines +7 to 22
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)
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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant