1

I need to make a countdown timer that displays a specific number of minutes and seconds counting down - not a countdown to a certain date.

And depending on a variable, change these numbers.

So for $video == 1, I need to display on the page: 8 minutes & 54 seconds (counting down)

And for $video == 2, I need to display on the page: 5 minutes & 01 seconds (counting down)

I also need the countdown display to disappear after the time has elapsed, but maybe I should put that into a different question.

The problem I'm having is the all the countdown scripts I can find deal with counting down to a specific date.

4
  • do you have code of what you are trying? Commented Jul 15, 2011 at 19:20
  • I've having a really hard time with this and not understanding any of the responses I'm getting. Maybe I can simply it for everyone... Is there a way for me to display in-line a minute and seconds countdown timer? It can be JS or PHP, as long as I can set the minutes and seconds right inside the code block. Then I can repeat it inside if statements for the $video variable. I know this is not the best way to do it, but nothing is making sense to me. Commented Jul 15, 2011 at 20:43
  • So, if someone could give me a self-contained (no libraries, no external scripts, etc) block of code that will display X min & Y sec (counting down), that would be awesome! Commented Jul 15, 2011 at 20:46
  • I'm abandoning this. It's out of my league. Thank you all for trying to help me. :) Commented Jul 15, 2011 at 21:15

5 Answers 5

2

Everything you need, just enter the total time in seconds in the <span> tags. 30 and 120 here for demo. Should work if you copy and paste directly into a webpage. Add and edit code as needed.

<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>
Sign up to request clarification or add additional context in comments.

Comments

1

Try:

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

4 Comments

I don't understand this at all. Is this JavaScript? Where are the variables for different videos? And how do I the countdown to display on the page?
@Dustin Yes, it is javascript. My code counts down 600 seconds (5 minutes). If you are wondering how, I'd suggest you lookup setInterval as I do not want to rob you of the learning experience. Adjusting the countdown time depending on $video must be trival from here.
I don't see how this displays on the page. And does it do minutes and seconds? Or just minutes?
@Dustin if there is a label defined in HTML like <label id='timer'>timer not started</label>, document.getElementById('timer').innerHTML = secs; would replace "timer not started" with number of seconds. If you want to display it as minutes and seconds, it is only a matter of simple math to do so.
1

There is a countdown script located at http://javascript.internet.com/time-date/countdown-timer.html that doesn't countdown to a date but rather a specified amount of minutes.

The code may be customized as follows to get the desired effect

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

Make sure that the contents of countDown.js looks like this:

/* 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;

2 Comments

This show 8:54, but it doesn't count down. Then it changes to 10:00 and it doesn't count down.
You might have copied the wrong information into countDown.js, I've edited the post to reflect that.
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>

Using the following library, you can implement a JQuery timer using the var countdownTime you specify above...

http://keith-wood.name/countdown.html <-- tutorial on the first page!

Edit Replaced $someTimeInSeconds with $countDownTime

1 Comment

I don't really understand how to use the library and I don't understand where the variable $someTimeinSeconds gets set.
0

Ok, I'm looking at doing something similar. Currently I have a simple countdown timer that is based off of current time that counts down every 30min. The problem is that I have to use a meta refresh to update it. I'm wondering if a combination of javascript and PHP might be a simpler solution to this answer. Use javascript to call the php code and automatically update it? Maybe set a variable for the time in the php script to be called with javascript? Well, here's the code I have that might help. I'm still learning.

$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>';

?>

I just have to figure out how to get it to reset itself at the end of every 30min and to update without meta refresh.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.