The jQuery timer I implemented is not showing up properly on my webpage

Here's a look at my HTML:

<!DOCTYPE html>
<html>
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
      <title>Event Website</title>
      <link rel="stylesheet" href="main.css">
      <script src="jquery-3.3.1.min.js"></script>
      <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet'  type='text/css'>
   </head>
   <body>
      <div class="row" id="horniRow">
         <div class="col-md-6">
            <h3 id="nameH3">
               Event Website
            </h3>
         </div>
         <div class="col-md-6 menu" id="menuDiv">
            <ul id="menuList">
               <li><a href="">HOME</a></li>
               <li><a href="">BLOG</a></li>
               <li><a href="">SPEAKERS</a></li>
               <li><a href="">SCHEDULE</a></li>
               <li><a href="">ATTENDING</a></li>
               <li><a href="">SIGN IN</a></li>
            </ul>
         </div>
      </div>
      <div class="row" id="countdown-row">
         <div class="col-md-2"></div>
         <div class="col-md-8 text-center">
            <p id="countdownTimer"></p>
         </div>
         <div class="col-md-2"></div>
      </div>
      <div class="row" id="dolniRow">
         <div class="col-md-8"></div>
         <div class="col-md-4 id="registerBox">
            <div class="" id="registerBox">
               <h2 style=""><u style="color: #e60000">REGISTER</u></h2>
               <div style=""></div>
            </div>
         </div>
      </div>
      <script src="countdown.js"></script>
   </body>
</html>

Take a look at the CSS I'm using:

 #countdownTimer {
     font-family: 'Open Sans', sans-serif;
     font-size: 700%;
     background-color:rgba(0, 0, 0, 0.3);
     color: red;
}
 body{
     background-image: url("/img/depo.jpg");
     width: 100%;
     margin: 0;
     overflow: hidden;
}
 #menuList{
     display: inline;
}
 #registerBox{
     background-color: #333333;
     position: fixed;
     right: 0px;
     bottom: 0px;
}
 ul {
     list-style-type: none;
     float: right;
     margin-top: 20px;
     margin-right: 20px;
}
 li {
     float: left;
     margin: 5px;
     text-decoration: none;
}
 #menuDiv {
     text-align: right;
}
 div ul li a {
     text-decoration: none;
     color: black;
}
 #nameH3 {
     margin-left: 10px;
}

This is the JavaScript/jQuery code in use:

var countDownDate = new Date("Sep 12, 2018 12:00:00").getTime();
// Update the count down every 1 second
var x = setInterval(function() {

  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now an the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Display the result in the element with id="demo"
  $("#countdownTimer").html(days + "d " + hours + "h " +
                            minutes + "m " + seconds + "s ")

  // If the count down is finished, write some text
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);

The countdown should be displayed in the middle of my page, as stated in my "countdown.js" file. However, it doesn't seem to be showing up. Both jQuery and the script file are correctly linked within my HTML structure, so I'm unsure why it's not working.

The following line should render the countdown timer inside the paragraph with the ID #countdownTimer:

$("#countdownTimer").html(days + "d " + hours + "h " + minutes + "m " + seconds + "s ");

And here is where the countdown timer should appear:

<p id="countdownTimer"></p>

Answer №1

Building upon @Arpit's response. The primary issue at hand is the inadequate linking of the Jquery framework, possibly due to a path error.

To avoid such problems in the future, it is highly recommended to utilize Content Delivery Network (CDN) servers to link any framework.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Another useful tip is to ensure that you insert this tag within your <body>, placed at the end of your HTML document.

This practice helps mitigate loading time issues.

Below is your code working as expected:

Warning: Be mindful that the id registerBox is used in two different elements which should ideally have unique IDs.

var countDownDate = new Date("Sep 12, 2018 12:00:00").getTime();
// Update the count down every 1 second
var x = setInterval(function() {

  // Get today's date and time
  var now = new Date().getTime();

  // Calculate the distance between now and the countdown date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes, and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Display the result in the element with ID "demo"
  $("#countdownTimer").html(days + "d " + hours + "h " +
                            minutes + "m " + seconds + "s ")

  // If the countdown is over, display appropriate text
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
 #countdownTimer {
     font-family: 'Open Sans', sans-serif;
     font-size: 700%;
     background-color:rgba(0, 0, 0, 0.3);
     color: red;
}
 body{
     background-image: url("/img/depo.jpg");
     width: 100%;
     margin: 0;
     overflow: hidden;
}
 #menuList{
     display: inline;
}
 #registerBox{
     background-color: #333333;
     position: fixed;
     right: 0px;
     bottom: 0px;
}
 ul {
     list-style-type: none;
     float: right;
     margin-top: 20px;
     margin-right: 20px;
}
 li {
     float: left;
     margin: 5px;
     text-decoration: none;
}
 #menuDiv {
     text-align: right;
}
 div ul li a {
     text-decoration: none;
     color: black;
}
 #nameH3 {
     margin-left: 10px;
}
<!DOCTYPE html>
<html>
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
      <title>Event Website</title>
      <link rel="stylesheet" href="main.css">
      <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet'  type='text/css'>
   </head>
   <body>
      <div class="row" id="horniRow">
         <div class="col-md-6">
            <h3 id="nameH3">
               Event Website
            </h3>
         </div>
         <div class="col-md-6 menu" id="menuDiv">
            <ul id="menuList">
               <li><a href="">HOME</a></li>
               <li><a href="">BLOG</a></li>
               <li><a href="">SPEAKERS</a></li>
               <li><a href="">SCHEDULE</a></li>
               <li><a href="">ATTENDING</a></li>
               <li><a href="">SIGN IN</a></li>
            </ul>
         </div>
      </div>
      <div class="row" id="countdown-row">
         <div class="col-md-2"></div>
         <div class="col-md-8 text-center">
            <p id="countdownTimer"></p>
         </div>
         <div class="col-md-2"></div>
      </div>
      <div class="row" id="dolniRow">
         <div class="col-md-8"></div>
         <div class="col-md-4 id="registerBox">
            <div class="" id="registerBox">
               <h2 style=""><u style="color: #e60000">REGISTER</u></h2>
               <div style=""></div>
            </div>
         </div>
      </div>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="countdown.js"></script>
   </body>
</html>

Answer №2

Make sure to include jQuery correctly in the head tag like this:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Instead of using:

<script src="jquery-3.3.1.min.js"></script>

Answer №3

Make sure to enclose your code within the document.ready function to ensure it executes correctly.

$(document).ready(function(){
    var countDownDate = new Date("Sep 12, 2018 12:00:00").getTime();

    // Update the countdown every second
    var x = setInterval(function() {

        // Get current date and time
       var now = new Date().getTime();

        // Calculate the distance between now and the countdown date
        var distance = countDownDate - now;

        // Perform time calculations for days, hours, minutes, and seconds
        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
        var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);

        // Display the resulting countdown in the element with id="countdownTimer"
        $("#countdownTimer").html(days + "d " + hours + "h "
        + minutes + "m " + seconds + "s ")

        // If the countdown has finished, display a message
        if (distance < 0) {
            clearInterval(x);
            document.getElementById("countdownTimer").innerHTML = "EXPIRED";
        }
    }, 1000);

});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to show HTML code from an HTML file in a Vue template?

I need help showcasing the HTML code from an external file in a Vue component template. <template> <div class="content"> <pre> <code> {{fetchCode('./code/code.html')}} & ...

Interactive mobile navigation featuring clickable elements within dropdown menus

I recently implemented a mobile nav menu based on instructions from a YouTube tutorial that I found here. Everything was working perfectly until I encountered an issue on the 'reviews list' page. The dropdown in the mobile nav is supposed to be ...

An error occurred in the main thread: java.lang.NullPointerException within the database project

Feeling puzzled as I attempt to view all the data in my database or insert some new data. Strangely, both methods are resulting in the same response. Here is my pojo class: public class Title { private Integer id; private String name; private String code ...

Troubles with Borders and Padding in HTML Elements

Hello everyone, I'm currently facing an issue trying to neatly fit a textbox, select menu, and button all within the same sized div. Each element seems to have strange borders/margins causing them not to display properly (the button ends up below the ...

Passing a jQuery variable to an MVC 3 Razor view HTML helper

I am currently working on a jQuery function that is embedded in an MVC Razor page. The function is designed to output a string based on the id attribute of checkboxes when they are checked or unchecked. <script type="text/javascript"> $(document ...

Determining the depth difference of nodes between two elements using JQuery

Is there a simple method to calculate the node depth difference between 2 elements? Example : <div id="1"> <div id="2"></div> <div id="3"> <div id="4"></div> </div> </div> <div id="5"></d ...

Jquery Flexigrid at your service

Can someone help me understand how to implement Flexigrid jQuery on my website? Your assistance is greatly appreciated. ...

Utilizing file uploading in combination with selected options for a streamlined experience

Currently, I am utilizing the jQuery FileUpload plugin created by BlueImp. The plugin utilizes x-tmpl for templating purposes. My aim is to populate a tags input field with options derived from a PHP variable. For now, I am experimenting by inserting place ...

What is another option for object-fit: cover?

Is there a way to resize an image on my webpage while maintaining its aspect ratio based on the screen size? I want the smaller dimension (width or height) to fit exactly within the element, with the larger dimension overflowing. I came across the objec ...

Adding elements in an asynchronous manner

I'm facing a challenge in generating elements based on query results and placing them inside the correct p containers. The problem is that all the elements are ending up in the last container instead of their respective positions. app.Controller.prot ...

Utilizing HighCharts in Your Project

View the graph here $(function () { var chart; $(document).ready(function() { chart = new Highcharts.Chart({ chart: { renderTo: 'container', type: 'column', margin: [ 50, 50, 100, 80] ...

Attempting to invoke selectize with an alternative parameter, however, it persists with the previous value

Trying to debug a function that takes a parameter and utilizes selectize.js. Initially, the function myFunc is called with a parameter value of 1. Everything works smoothly as data is fetched from the server and displayed in a dropdown list. However, upo ...

I'm encountering inexplicable duplications of elements on my Wordpress site

Here is an example of my template header: <header> <?php if ( function_exists( 'jetpack_the_site_logo' ) ) jetpack_the_site_logo(); ?> <a class="menu-toggle">menu</div> <?php wp_nav_menu( array('them ...

Performing a cross-domain JSON Get request with JQuery

Struggling with a simple JSON get request to an API on a domain that I don't have control over. Here's the code I'm using: $(document).ready(function () { $.ajax({ type: 'GET', url: 'http://pu ...

Having trouble with JavaScript concat function not behaving as it should? Can you provide more details to

I am trying to extract all the cities from an object that contains country names as keys and arrays of cities as values. However, my approach seems to be failing and I can't figure out why. var cities = { "United Kingdom": ['london'], ...

Using I18Next fails to function properly while trying to retrieve data from the backend

After integrating i18next into my application and fetching data from the backend (i18next-xhr-backend), I encountered an issue. Although there were no errors, the translation was not functioning as expected. I am using Vue along with Webpack. Below is a s ...

Data retrieval from client-side fetch request results in a corrupted file upon download

I'm facing an issue while attempting to fetch a file using a GET request and download it directly in the browser. However, after the download is complete and I try to open the file, it seems to be corrupted. Strangely, only .txt files are opening corr ...

Conceal the div element if the screen size drops below a certain threshold

Is it possible to hide a div element when the browser width is less than or equal to 1026px using CSS, like this: @media only screen and (min-width: 1140px) {}? If not, what are some alternative solutions? Additional information: When hiding the div eleme ...

Mongoose: utilize populate for fetching records; if not, return an array of records

Learning MeanJS and encountering Mongoose issues. Two models are in question: CategorySchema: - name: String, default:'', required: 'Please fill Category name', trim: true - slug: String, default:'', trim: true, unique: tr ...

Exploring the Differences: innerHTML versus appendChild for Loading Scripts

Struggling to dynamically load scripts onto the DOM? function addScript(fileName) { document.body.innerHTML += `<script src='components/${fileName}/${fileName}.js'></script>` } addScript('message-interface') I prefer th ...