HTML JavaScript 基于计时器的条件 href link

HTML JavaScript Conditional href link based on timer

我正在尝试根据条件将用户重定向到特定页面。 If Timer still 运行 open booknow.html if timer expired open booknowcode.html

伪代码

  1. 计时器还在运行

  1. 用户点击“立即预订”按钮 如果条件: a) 如果计时器仍然 运行 -> 将用户发送到 booknowcode.html 页面 b) 如果计时器过期 -> 将用户发送到 booknow.html 页面

// 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>

那么如何根据定时器条件将booknow.html替换为booknowcode.html呢?

您应该设置 href 而不是 innerHTML

document.querySelector("#conditional-button a").href = link;

像这样

版本 1

  1. 删除按钮并使其成为 link
  2. 给link一个ID
  3. 计时器到期时更改为document.getElementById("link").href = "booknowcode.html";

// 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>

版本 2

  1. 删除按钮周围的 link 并使其成为按钮
  2. 点击按钮时添加一个事件监听器
  3. 在点击处理程序中测试距离
  4. 改变document.getElementById("demo").innerHTML = link; location = link 当 运行

// 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>

只需将您的新 link 应用到 <a>(我给它一个数据属性以使我们的替换准确)

if (distance <= 0) {
  // ....
  document.querySelector('a[data-rel=book-link]').setAttribute('href', link)

我不确定为什么锚标记内有一个按钮,所以我删除了该按钮并将 css 应用于 a,它看起来应该是一样的。

// Set the date we're counting down to
//var countDownDate = new Date("June 30, 2021 09:17:25").getTime();
countDownDate = new Date().getTime() + 10000;
// 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%";
  let link;

  // If the count down is over, write Expired text 
  if (distance <= 0) {
    clearInterval(x);
    document.getElementById("timer").innerHTML = "EXPIRED";
    link = "booknow.html";
    document.querySelector('a[data-rel=book-link]').setAttribute('href', link)

  //===if timer still running then redirect to dicount page (distance more than zero) Start ===
  } 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 data-rel='book-link' href="booknow.html" class="btn btn-info">Book Now</a>
</div>

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