在 .. 倒计时器 PHP 中显示重定向

发布于 2024-10-10 06:46:47 字数 216 浏览 6 评论 0原文

到目前为止,我有这段代码,它会在 5 秒后将用户重定向到正确的 URL:

请您告诉我如何显示一个倒计时器,其中显示“正在重定向..”,其中“..”是剩余的秒数。我是网络开发新手,所以所有代码都会有所帮助!

I have this code so far that redirects the user after 5 seconds to the correct URL:

<?php
$url = $_GET['url'];
header("refresh:5;url=$url");
include('ads.php');
?>

Please could you tell me how i could display a countdown timer saying Redirecting In.. with .. being the amount of seconds left. I am new to web development so all code will be helpful!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

软糖 2024-10-17 06:46:47
<script type="text/javascript">

(function () {
    var timeLeft = 5,
        cinterval;

    var timeDec = function (){
        timeLeft--;
        document.getElementById('countdown').innerHTML = timeLeft;
        if(timeLeft === 0){
            clearInterval(cinterval);
        }
    };

    cinterval = setInterval(timeDec, 1000);
})();

</script>

Redirecting in <span id="countdown">5</span>.

你可以试试这个。

<script type="text/javascript">

(function () {
    var timeLeft = 5,
        cinterval;

    var timeDec = function (){
        timeLeft--;
        document.getElementById('countdown').innerHTML = timeLeft;
        if(timeLeft === 0){
            clearInterval(cinterval);
        }
    };

    cinterval = setInterval(timeDec, 1000);
})();

</script>

Redirecting in <span id="countdown">5</span>.

You can try this.

喜爱皱眉﹌ 2024-10-17 06:46:47

因为这是初学者常见的问题;我只是想强调,为了最佳实践,setInterval 应该并且可以通常可以通过在函数内递归使用 setTimeout 来避免。

例如:

var timer = 5,
    el = document.getElementById('countdown');

(function t_minus() {
    'use strict';
    el.innerHTML = timer--;
    if (timer >= 0) {
        setTimeout(function () {
            t_minus();
        }, 1000);
    } else {
        // do stuff, countdown has finished.
    }
}());

As this is a common beginner question; I just wanted to highlight that for best practise setInterval should, and can usually be avoided by using setTimeout recursively within a function.

For example:

var timer = 5,
    el = document.getElementById('countdown');

(function t_minus() {
    'use strict';
    el.innerHTML = timer--;
    if (timer >= 0) {
        setTimeout(function () {
            t_minus();
        }, 1000);
    } else {
        // do stuff, countdown has finished.
    }
}());
风吹雨成花 2024-10-17 06:46:47

凯尔的优秀代码。我已经用暂停和恢复按钮修改了计时器。

<HTML>
<HEAD>
    <SCRIPT LANGUAGE="JavaScript">

    var time_left = 50;
    var cinterval;
    var timestatus=1;

    function time_dec(){
        time_left--;
        document.getElementById('countdown').innerHTML = time_left;
        if(time_left == 0){
            clearInterval(cinterval);
        }
    }

    function resumetime()
    {
        //time_left = 50;
        clearInterval(cinterval);
        cinterval = setInterval('time_dec()', 1000);
    }

    function defaultstart()
    {
        time_left = 50;
        clearInterval(cinterval);
        cinterval = setInterval('time_dec()', 1000);
    }

    function stopstarttime()
    {
        if(timestatus==1)
    {
        clearInterval(cinterval);
        document.getElementById('stopbutton').value="Start";
        timestatus=0;
    }
        else
    {
        clearInterval(cinterval);
        cinterval = setInterval('time_dec()', 1000);
        document.getElementById('stopbutton').value="Stop";
        timestatus=1;
    }
    }

    defaultstart();

    </SCRIPT>
</HEAD>

<body>
    Redirecting In <span id="countdown">50</span>.
    <INPUT TYPE="button" value="stop" id="stopbutton" onclick="stopstarttime()">
</body>
</HTML>

Excellent code by Kyle. I have modified the timer with a pause and resume button.

<HTML>
<HEAD>
    <SCRIPT LANGUAGE="JavaScript">

    var time_left = 50;
    var cinterval;
    var timestatus=1;

    function time_dec(){
        time_left--;
        document.getElementById('countdown').innerHTML = time_left;
        if(time_left == 0){
            clearInterval(cinterval);
        }
    }

    function resumetime()
    {
        //time_left = 50;
        clearInterval(cinterval);
        cinterval = setInterval('time_dec()', 1000);
    }

    function defaultstart()
    {
        time_left = 50;
        clearInterval(cinterval);
        cinterval = setInterval('time_dec()', 1000);
    }

    function stopstarttime()
    {
        if(timestatus==1)
    {
        clearInterval(cinterval);
        document.getElementById('stopbutton').value="Start";
        timestatus=0;
    }
        else
    {
        clearInterval(cinterval);
        cinterval = setInterval('time_dec()', 1000);
        document.getElementById('stopbutton').value="Stop";
        timestatus=1;
    }
    }

    defaultstart();

    </SCRIPT>
</HEAD>

<body>
    Redirecting In <span id="countdown">50</span>.
    <INPUT TYPE="button" value="stop" id="stopbutton" onclick="stopstarttime()">
</body>
</HTML>
扶醉桌前 2024-10-17 06:46:47

这是我的看法,没有函数外部的变量。取决于 jQuery。

function count_down_to_action(seconds, do_action, elem_selector)
{
    seconds = typeof seconds !== 'undefined' ? seconds : 10;
    $(elem_selector).text(seconds)
    var interval_id = setInterval(function(){
        if (seconds <= 0)
        {
            clearInterval(interval_id);
            if (typeof do_action === 'function') 
                do_action();
        }
        else
            $(elem_selector).text(--seconds);
    },1000)
}

这是一个使用它的示例 http://jsfiddle.net/VJT9d/

Here is my take on it, without variables outside of the function. Depends on jQuery.

function count_down_to_action(seconds, do_action, elem_selector)
{
    seconds = typeof seconds !== 'undefined' ? seconds : 10;
    $(elem_selector).text(seconds)
    var interval_id = setInterval(function(){
        if (seconds <= 0)
        {
            clearInterval(interval_id);
            if (typeof do_action === 'function') 
                do_action();
        }
        else
            $(elem_selector).text(--seconds);
    },1000)
}

Here is an example using it http://jsfiddle.net/VJT9d/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文