简体   繁体   English

在特定时间后重定向到另一个页面并显示倒计时

[英]Redirect to Another page after specific time with countdown showing

I'm trying to redirect this button after specific time.我正在尝试在特定时间后重定向此按钮。 But the countdown couldn't start before redirecting.但是在重定向之前无法开始倒计时。 I wish it to be redirect after 5 or 10 seconds.我希望它在 5 或 10 秒后重定向。 But remember window.location should be in HTML Element.但请记住 window.location 应该在 HTML 元素中。

 function myFunction(){ var button=document.getElementById('button'); button.addEventListener('click', function(){ var i=0; var tt=setInterval(function (){ i=i+1; var counter=5-i; button.innerHTML='You Will Be Redirect After:'+counter; if(counter===0){ clearInterval(tt); } },1000); });};
 <button id="button"> <a href="#" class="butstyle" id="button1" onclick="myFunction(window.location='https://google.com')" >Click To Redirect</a> </button>

You can put the var i outside the function.您可以将 var i放在 function 之外。 So that counter is incremented as well.所以这个counter也会增加。

Also do not use inline event Handlers (where possible) - Redirect only when the condition is met in your if condition.也不要使用内联事件处理程序(如果可能) - 仅当您的if条件满足条件时才Redirect

Pass the redirect URL as an argument and access that in myFunction(url)将重定向URL作为参数传递并在myFunction(url)中访问它

Demo:演示:

 var i = 0; function myFunction(url) { var tt = setInterval(function() { i = i + 1; var counter = 6 - i; button.innerHTML = 'You Will Be Redirect After:' + counter; if (counter === 0) { clearInterval(tt); window.location = url //redirect here } }, 1000); };
 <button id="button"> <a href="#" class="butstyle" id="button1" onclick="myFunction('https://google.com')" >Click To Redirect</a> </button>

I would do something like this.我会做这样的事情。 5 is the amount of seconds you want to redirect after. 5 是您想要重定向的秒数。

document.getElementById('btn').addEventListener("click", () => {
  let i = 1;
  setInterval(() => {
    if(i == 5){
      window.location.href = "https://google.com"
    }
    document.getElementById('btn').innerHTML = `Redirecting in ${5 - i} seconds`
    i++;
  }, 1000);
});
<button id="btn">
  Click to redirect
</button>

I would take the 2020 option using the "data" attributes, see the below example.我会使用“数据”属性选择 2020 选项,请参见下面的示例。 the naming of the attributes should be descriptive enough.属性的命名应该具有足够的描述性。 Let me know if there are any questions.如果有任何问题,请告诉我。

 <button id="button" onclick="myFunction(this)" data-secondstoredirect="5" data-urltoredirect="https://google.com">Click To Redirect</button> <script> function myFunction(e) { e = e || window.event; var $this = e.target || e.srcElement || e; if ($this.nodeType == 3) $this = $this.parentNode; // defeat Safari bug, https://stackoverflow.com/questions/1553661/how-to-get-the-onclick-calling-object var secondstoredirect = $this.getAttribute("data-secondstoredirect"), redirectAfterSeconds = (secondstoredirect * 1000), urlToRedirect = $this.getAttribute("data-urltoredirect"); $this.innerHTML = 'You Will Be Redirect After: ' + redirectAfterSeconds; var tt = setInterval(function () { window.location.href = urlToRedirect; }, redirectAfterSeconds); } </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM