dongweiben5229
dongweiben5229
2012-09-21 18:06

PHP显示焦点实时倒计时(20秒)[重复]

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • douba1904 douba1904 9年前

    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!";
    }
    
    点赞 评论 复制链接分享
  • duangewu5234 duangewu5234 9年前

    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.

    点赞 评论 复制链接分享
  • dongshan1036 dongshan1036 9年前

    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.

    点赞 评论 复制链接分享
  • doolo00026 doolo00026 9年前

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

    点赞 评论 复制链接分享

为你推荐