0

Is there a way to make a countdown script with PHP?

4shared.com or megaupload.com use similar script, and it starts to count from 20 seconds to 0 second.

I can clearly see how many seconds left on the web page until I can click download page.

Is this jquery?

0

4 Answers 4

2

This is with a simple javascript timer. It's just as easy with native javascript as it is with jQuery (or any other library):

<script type="text/javascript">
var seconds_left = 20;
var interval = setInterval(function() {
    document.getElementById('timer_div').innerHTML = --seconds_left;
    if (seconds_left == 0)
    {
        clearInterval(interval);
        alert('do something here!');
    }
}, 1000);
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

it needs to delete "clearInterval(inverval);" line to make it work.
Yeah, I had a typo. It needs to be clearInterval(interval)
1

the counter is a javascript feature

after that the refresh header is sent using the header function to redirect theuser after a number of seconds

Update:

  header( "refresh:3;url=other.php" );
  echo 'You will be redirected in 3 secs. If not, click <a href="other.php">here</a>.';

this is how you redirect after a number of seconds

Comments

1

You will have to use both the JavaScript and PHP for that.

JavaScript will show the countdown on the user's browser and will send a request to the PHP script once the desired time has elapsed. JQuery or any other framework can be used for that reason.

PHP script will then check for the time gap and will out put the file if the time requirements are satisfied.

Comments

1

You need to mix up PHP & Javascript for this

First PHP

Send headers, so that page will navigate to the download page .

header( "refresh:30;url=download.php" );
echo 'The download will begin in 30 secs. If not, click <a href="download.php">here</a>.';

Then Javascript

This handles the part where we show time to the user, so that he know.

var count=30; //30 seconds
var counter = setInterval(timer(),1000); //1000 will  run it every 1 second
function timer()
{
  count=count-1;
  if (count <= 0)
  {
     clearInterval(counter);
     return;
  }

  document.getElementById("placetoshowtime").innerHTML = count; //display the time to user
}

Small demo of JS Part.

When, the java script finishes ticking, the PHP will already have redirected to download page. So it creates the illusion you are after.

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.