Search code examples
javascripthtmlcountdown

Adding a Button to a Javascript Countdown


I'm incredibly new to Javascript and don't know it very well. However, I've managed to create a countdown timer that semi-works like I want it to. It's pretty simple but it's basically a timer that counts down to a specific date, and then once it reaches the specified date and time, it then displays text that I can customize. I would love for this code be able to display a button with a hyperlink once the countdown reaches zero. Here is the code that I have so far:

// Set the date we're counting down to
var countDownDate = new Date(Date.now() + 20000).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 and 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);

  // Output the result in an element with id="demo"
  document.getElementById("demo").innerHTML = days + " days, " + hours + " hours, " +
    minutes + " minutes, & " + seconds + " seconds";

  // If the count down is over, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "We're Live on Facebook!";
  }
}, 1000);
<p id="demo" class="countdown-live" style="text-align:center;"></p>

Any help getting this to display a hyperlinked button instead of the text "We're Live On Facebook!" would be greatly appreciated.


Solution

  • Just add the HTML to the string you are setting:

    // Set the date we're counting down to
    var countDownDate = new Date(Date.now() + 20000).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 and 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);
    
      // Output the result in an element with id="demo"
      document.getElementById("demo").innerHTML = days + " days, " + hours + " hours, " +
        minutes + " minutes, & " + seconds + " seconds";
    
      // If the count down is over, write some text 
      if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo").innerHTML = '<a href="https://facebook.com">We\'re Live on Facebook!</a>';
      }
    }, 1000);
    <p id="demo" class="countdown-live" style="text-align:center;"></p>