Search code examples
javascripthtmlif-statementbuttonhref

HTML JavaScript Conditional href link based on timer


I am trying to re-direct user to a specific page based on condition. If Timer still running open booknow.html if timer expired open booknowcode.html

Pseudo code

  1. Timer still running

enter image description here

  1. User clicks Book Now button If condition : a) if timer still running -> send user to booknowcode.html page b) if timer expired -> send user to booknow.html page enter image description here

// Set the date we're counting down to
var countDownDate = new Date("June 30, 2021 09:17:25").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get today's 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="timer"
  document.getElementById("timer").innerHTML = days + "d " + hours + "h " +
    minutes + "m " + seconds + "s " + "Discount 15%";

  // If the count down is over, write Expired text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("timer").innerHTML = "EXPIRED";
  }
  //===if timer still running then redirect to dicount page (distance more than zero) Start ===
  let link;

  if (distance <= 0) {
    link = "booknow.html";
  } else {
    link = "booknowcode.html";
  }
  document.getElementById("demo").innerHTML = link;
  //===if timer still running then redirect to dicount page (distance more than zero) End ===

}, 1000);
<div class="timer-button" id="conditional-button">
  <p id="timer"></p>
  <a href="booknow.html">
    <button class="btn btn-info" type="button" onclick="">Book Now</button>
  </a>
</div>

<p id="demo"></p>

So how do I replace booknow.html with booknowcode.html based on the timer condition?


Solution

  • Like this

    Version 1

    1. Remove the button and make it a link
    2. give the link an ID
    3. change to document.getElementById("link").href = "booknowcode.html"; when timer expires

    // Set the date we're counting down to
    var countDownDate = new Date("June 30, 2021 09:17:25").getTime();
    // Get today's date and time
    var now = new Date().getTime();
    
    // Find the distance between now and the count down date
    var distance = countDownDate - now;
    
    
    // Update the count down every 1 second
    var x = setInterval(function() {
    
      // Get today's date and time
      now = new Date().getTime();
    
      // Find the distance between now and the count down date
      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="timer"
      document.getElementById("timer").innerHTML = days + "d " + hours + "h " +
        minutes + "m " + seconds + "s " + "Discount 15%";
    
    // If the count down is over, write Expired text 
      if (distance < 0) {
        clearInterval(x);
        document.getElementById("timer").innerHTML = "EXPIRED";
    
      //===if timer stops, the link changes  ===
    
        document.getElementById("link").href = "booknowcode.html";
      }
    
    
    
    
    }, 1000);
    <div class="timer-button" id="conditional-button">
      <p id="timer"></p>
      <a href="booknow.html" class="btn btn-info" id="link">Book Now</button>
    </div>

    Version 2

    1. Remove the link around the button and make it a button
    2. Add an eventListener on the click of the button
    3. Test the distance in the click handler
    4. change document.getElementById("demo").innerHTML = link; to location = link when running

    // Set the date we're counting down to
    var countDownDate = new Date("June 30, 2021 09:17:25").getTime();
    // Get today's date and time
    var now = new Date().getTime();
    
    // Find the distance between now and the count down date
    var distance = countDownDate - now;
    
    
    // Update the count down every 1 second
    var x = setInterval(function() {
    
      // Get today's date and time
      now = new Date().getTime();
    
      // Find the distance between now and the count down date
      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="timer"
      document.getElementById("timer").innerHTML = days + "d " + hours + "h " +
        minutes + "m " + seconds + "s " + "Discount 15%";
    
      // If the count down is over, write Expired text 
      if (distance < 0) {
        clearInterval(x);
        document.getElementById("timer").innerHTML = "EXPIRED";
      }
      //===if timer still running then redirect to dicount page (distance more than zero) Start ===
      let link;
    
    
    
    }, 1000);
    document.getElementById("but").addEventListener("click", function() {
      if (distance <= 0) {
        link = "booknow.html";
      } else {
        link = "booknowcode.html";
      }
      document.getElementById("demo").innerHTML = link;
      // change this to 
      // location  = link; 
      // when running
    
      //===if timer still running then redirect to dicount page (distance more than zero) End ===
    })
    <div class="timer-button" id="conditional-button">
      <p id="timer"></p>
      <button class="btn btn-info" id="but" type="button">Book Now</button>
    </div>
    
    <p id="demo"></p>