开发者

Show Redirecting In.. CountDown Timer PHP

开发者 https://www.devze.com 2023-02-02 15:10 出处:网络
I have this code so far that redirects the user after 5 seconds to the correct URL: <?php $url = $_GET[\'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 ti开发者_开发技巧mer saying Redirecting In.. with .. being the amount of seconds left. I am new to web development so all code will be helpful!


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


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.
    }
}());


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>


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消