本文介绍了PHP,如何创建倒数计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何做到这一点:

您将在 5..(4,3,2,1) 秒后重定向到 domain.com"

"you will be redirected to domain.com in 5..(4,3,2,1) seconds"

在 php 中???

推荐答案

我很确定 javascript 是您的最佳选择.唯一的另一种方法是每秒重定向到一个新的 URL,在我看来这太过分了.

I'm pretty sure javascript is your best option. The only other way to do it would be to redirect to a new URL every second, which is overkill in my opinion.

以下是倒计时示例的完整源代码:

Here's full source code for a sample countdown:

<html>
  <head>
    <meta http-equiv="refresh" content="5;url=http://example.com" />
    <title>Countdown Sample</title>
  </head>
  <body>
    you will be redirected to example.com in <span id="seconds">5</span>.
    <script>
      var seconds = 5;
      setInterval(
        function(){
          document.getElementById('seconds').innerHTML = --seconds;
        }, 1000
      );
    </script>
  </body>
</html>

编辑

这是一个包含 Alnitak 建议的改进版本:

Edit

Here's an improved version with Alnitak's advice:

我更改了 JavaScript 以重定向用户并防止倒计时低于 1,并且我在 周围添加了一个 标记> 对于没有 JavaScript 的用户.

I've changed the JavaScript to redirect users and prevent the countdown from going below 1 and I've added a <noscript> tag around the <meta> for users without JavaScript.

<html>
  <head>
    <noscript>
      <meta http-equiv="refresh" content="5;url=http://example.com" />
    </noscript>
    <title>Countdown Sample</title>
  </head>
  <body>
    you will be redirected to example.com in <span id="seconds">5</span>.
    <script>
      var seconds = 5;
      setInterval(
        function(){
          if (seconds <= 1) {
            window.location = 'http://example.com';
          }
          else {
            document.getElementById('seconds').innerHTML = --seconds;
          }
        },
        1000
      );
    </script>
  </body>
</html>

这篇关于PHP,如何创建倒数计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:10