PHP JavaScript倒数计时器

时间:2011-07-15 19:18:12

标签: php javascript countdown

我需要制作一个倒计时器,显示一个特定的分钟数和秒数 - 而不是某个特定日期的倒计时。

根据变量,更改这些数字。

因此对于$video == 1,我需要在页面上显示:8 minutes & 54 seconds(倒计时)

对于$video == 2,我需要在页面上显示:5 minutes & 01 seconds(倒计时)

我还需要倒计时显示在经过一段时间后消失,但也许我应该把它放到另一个问题中。

我遇到的问题是我能找到的所有倒计时脚本都可以计算到特定日期。

5 个答案:

答案 0 :(得分:2)

您需要的所有内容,只需输入<span>代码中的总时间(以秒为单位)即可。 30120在这里进行演示。如果您直接复制并粘贴到网页中,则应该可以正常工作。根据需要添加和编辑代码。

<span id="countdown-1">30 seconds</span>
<span id="countdown-2">120 seconds</span>
<script type="text/javascript">
    // Initialize clock countdowns by using the total seconds in the elements tag
    secs       = parseInt(document.getElementById('countdown-1').innerHTML,10);
    setTimeout("countdown('countdown-1',"+secs+")", 1000);
    secs       = parseInt(document.getElementById('countdown-2').innerHTML,10);
    setTimeout("countdown('countdown-2',"+secs+")", 1000);

    /**
     * Countdown function
     * Clock count downs to 0:00 then hides the element holding the clock
     * @param id Element ID of clock placeholder
     * @param timer Total seconds to display clock
     */
    function countdown(id, timer){
        timer--;
        minRemain  = Math.floor(timer / 60);
        secsRemain = new String(timer - (minRemain * 60));
        // Pad the string with leading 0 if less than 2 chars long
        if (secsRemain.length < 2) {
            secsRemain = '0' + secsRemain;
        }

        // String format the remaining time
        clock      = minRemain + ":" + secsRemain;
        document.getElementById(id).innerHTML = clock;
        if ( timer > 0 ) {
            // Time still remains, call this function again in 1 sec
            setTimeout("countdown('" + id + "'," + timer + ")", 1000);
        } else {
            // Time is out! Hide the countdown
            document.getElementById(id).style.display = 'none';
        }
    }
</script>

答案 1 :(得分:1)

 <?php 
 $countDownTime = 0;
 if ($video == 1) $countDownTime = (8*60 + 54);
 else if ($video == 2) $countDownTime = (5*60 + 1);
 echo '<script>var countdownTime="' . $countDownTime . '";</script>"'; 
 ?>

 <script>
 <!-- as per the hyper linked reference below -->
 $(selector).countdown({until: countdownTime});
 </script>

使用以下库,您可以使用上面指定的var countdownTime实现JQuery计时器...

http://keith-wood.name/countdown.html&lt; - 第一页上的教程!

编辑用$ countDownTime替换$ someTimeInSeconds

答案 2 :(得分:1)

尝试:

var x, secs = 600; //declared globally

x = setInterval(myFunc, 1000);

function myFunc()
{
    document.getElementById('timer').innerHTML =  secs; //assuming there is a label with id 'timer'
    secs --;
    if(secs == 0)
    {
        document.getElementById('timer').style.hidden = true;
        clearInterval(x);
    }
}

答案 3 :(得分:1)

位于http://javascript.internet.com/time-date/countdown-timer.html的倒计时脚本不会倒计时,而是指定的分钟数。

可以按如下方式自定义代码以获得所需的效果

<?php
 if ($video===1){
    $time="8:54";
 }
 if ($video===2){
    $time="5:01";
 }


?>

<script type="text/javascript" src="countDown.js"></script>

    <form name="cd">
    <input id="txt" readonly="true" type="text" value="<?php echo $time; ?>" border="0" name="disp">
    </form>

确保countDown.js的内容如下所示:

/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Neill Broderick :: http://www.bespoke-software-solutions.co.uk/downloads/downjs.php */

var mins
var secs;

function cd() {
    mins = 1 * m("10"); // change minutes here
    secs = 0 + s(":01"); // change seconds here (always add an additional second to your total)
    redo();
}

function m(obj) {
    for(var i = 0; i < obj.length; i++) {
        if(obj.substring(i, i + 1) == ":")
        break;
    }
    return(obj.substring(0, i));
}

function s(obj) {
    for(var i = 0; i < obj.length; i++) {
        if(obj.substring(i, i + 1) == ":")
        break;
    }
    return(obj.substring(i + 1, obj.length));
}

function dis(mins,secs) {
    var disp;
    if(mins <= 9) {
        disp = " 0";
    } else {
        disp = " ";
    }
    disp += mins + ":";
    if(secs <= 9) {
        disp += "0" + secs;
    } else {
        disp += secs;
    }
    return(disp);
}

function redo() {
    secs--;
    if(secs == -1) {
        secs = 59;
        mins--;
    }
    document.cd.disp.value = dis(mins,secs); // setup additional displays here.
    if((mins == 0) && (secs == 0)) {
        window.alert("Time is up. Press OK to continue."); // change timeout message as required
        // window.location = "yourpage.htm" // redirects to specified page once timer ends and ok button is pressed
    } else {
        cd = setTimeout("redo()",1000);
    }
}

function init() {
  cd();
}
window.onload = init;

答案 4 :(得分:0)

好的,我正在做类似的事情。目前我有一个简单的倒数计时器,它基于当前时间,每30分钟倒计时一次。问题是我必须使用元刷新来更新它。我想知道javascript和PHP的组合是否可能是这个答案的更简单的解决方案。使用javascript调用php代码并自动更新它?也许在php脚本中设置一个变量用javascript调用?好吧,这是我可能有用的代码。我还在学习。     

$minutes_left = ($minutes)?((30 - $minutes)-(($seconds)?1:0)):0;
$minutes_left = str_pad ($minutes_left , 2, '0', STR_PAD_LEFT);

$seconds_left = ($seconds)?(60 - $seconds):0;
$seconds_left = str_pad ($seconds_left , 2, '0', STR_PAD_LEFT);

echo '<center><h1 style="font-color:white;">Next station break in: '.$minutes_left.'m '.$seconds_left.'s</h2></center>';

?>

我必须弄清楚如何让它在每30分钟结束时重置自己,并在没有元刷新的情况下进行更新。