0

Possible Duplicate:
Javascript countdown timer that stops when window is not in focus

I have been searching for a solution to this matter.

What I want to do is this : When a visitor clicks on a specific link on the website, he gets redirected to another page.

On this other page, he has to stay for 20 seconds. But I want to apply a countdown , so he sees the timer going down from 20 seconds to 0 seconds.

and when the timer hits 0 seconds, I will execute a php script. (it's in place)

Now , wt I also want is , that the timer will keep ticking as long as the visitor is on this page, (if this page is in focus) , else the timer stops, till he's back on the page

I have searched a lot to find a solution , but I don't even know if it's doable with php.

Any help to atleast let me know where to search will be very appreciated

thank you a lot in advance

Community
  • 1
  • 1
Shady
  • 1,701
  • 2
  • 13
  • 14
  • Of course that it is not doable with PHP. All work on the client's device must be done through javascript or something that compiles to it like elm, flapjax or coffeescript. –  Sep 21 '12 at 18:09
  • Something like: http://stackoverflow.com/questions/5766263/run-settimeout-only-when-tab-is-active ? – Bryan Sep 21 '12 at 18:09
  • Great another adf.ly clone, lol ;p Na Anything like this can be bypassed, unless you increment a count on the server. – Lawrence Cherone Sep 21 '12 at 18:12
  • @Bryan am kinda imparessed to say this, but How do I call this function in a php page? I put – Shady Sep 21 '12 at 18:23
  • I am not a js expert, your answer lies in the code for the demo: http://jsfiddle.net/simevidas/J68dJ/ – Bryan Sep 21 '12 at 18:26

4 Answers4

0

It's not doable in PHP. PHP is a server-side language, so you request a page, it's compiled and served.

You'll need JavaScript. What you'll need to do is break the task down into steps. So you know that:

  1. There should be a 20-second countdown
  2. A script should be executed at the end of that countdown

So, your first step would be to look at the setTimeout() function in JavaScript. Then you want the callback function to either redirect, or call another URL via XMLHttpRequest (AJAX).

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • Ok , so I need JavaScript, ok and focus issue, can be solved in javascript aswell I assume? , the problem is am a php developer only have no experience in Javascript, but will do my best to get it working. Thank you very much. – Shady Sep 21 '12 at 18:11
  • I'm not sure what you mean by focus. A user could open the page, and then just switch to a different tab whilst the page counts down in the background could they not? – Martin Bean Sep 21 '12 at 18:15
0

I think this will do the job, you only have to include the jQuery library before this script. Writing your script in jQuery is a lot easier than pure javascript. Personaly I rarely use pure javascript.

Community
  • 1
  • 1
Manolis Agkopian
  • 1,033
  • 13
  • 22
0

Shady - Why? Are not web sites suppose to be interactive to humans?

You could probably do it with Javascript, but one feels that this will annoy your users/cusomters/clients/employess.

PS: PHP Just runs on the server.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

What you need to do in PHP is decide the time that the timer has begun, for instance when a page is requested. Let's say the page is download.php and the code looks something like this:

session_start(); //this has to be before you echo any text out or send any headers
$_SESSION['minimum_time'] = time() + 20;

Then in the HTML that the page outputs you can show a JavaScript countdown timer. You should just search for one that is simple and fits your application.

Make the JS timer countdown for 20 seconds as well. Perhaps add an extra second here, or subtract one from the PHP side just in case, but in all likelihood users will always be at LEAST 20 seconds past the start time by the time the JS timer ends due to the network time and page rendering.

After the JS countdown redirect the user or present them with a link to the new page, let's call it file.php, it should have something like this:

if (time() > $_SESSION['minimum_time']) {
  print "Thanks for waiting!";
}
thewebguy
  • 1,510
  • 10
  • 15