最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PHP Timer wait 30 seconds then run a command - Stack Overflow

programmeradmin5浏览0评论

I'm just looking for a simple timer, where I can get my page to run a script after 30 seconds.

The idea is that the user has 30 seconds to submit an answer, otherwise the page will run a script and take them to a 'sorry, too slow' style page.

I cannot find the correct php function for this, but it basically we be like:

<?php

Start timer(30);

when timer runs out:
header("location: tooslow.php");
?>

Thanks for any help, Brett

I'm just looking for a simple timer, where I can get my page to run a script after 30 seconds.

The idea is that the user has 30 seconds to submit an answer, otherwise the page will run a script and take them to a 'sorry, too slow' style page.

I cannot find the correct php function for this, but it basically we be like:

<?php

Start timer(30);

when timer runs out:
header("location: tooslow.php");
?>

Thanks for any help, Brett

Share Improve this question edited Jan 20, 2012 at 14:18 Shiplu Mokaddim 57.7k20 gold badges145 silver badges192 bronze badges asked Jan 20, 2012 at 14:07 Brett StirlingBrett Stirling 971 gold badge2 silver badges8 bronze badges 1
  • 3 You can't issue a header statement after sending html (body) content. Timer would have to be implemented in Javascript (but you can validate using PHP) – symcbean Commented Jan 20, 2012 at 14:19
Add a ment  | 

9 Answers 9

Reset to default 6

Store the page request time (from the time() function) in a session variable, then subtract the request time from the submission time (again, from the same function) when the user posts the answer. If the difference is larger than 30, they took too long.

Pseudocode:

<?php
session_start();
$_SESSION['question_start'] = time();
echo "Question: How long is a piece of string?";
...
?>

Then, have that page post to another, which does this:

<?php
session_start();
$time = time() - $_SESSION['question_start'];
if($time > 30)
{
    // fail!
}
...
?>

Of course, if you want the jump to the page to be automatic, you can use the above method in conjunction with one of the JavaScript methods in the other answers here. However, JavaScript alone provides absolutely zero guarantee that the user solved the question within the time limit, as you can just disable JavaScript.

My personal suggestion would be that you don't use a page redirect at all, but instead use Ajax to provide an interactive interface. Essentially, you use a setTimeout call as usual, but when it pletes it does an Ajax request to a checking script written in PHP. That script would implement the session timing logic I provided above. If the time is OK, the script increases their score if they got it correct, then responds with the next question, which can be displayed on the page. If the time was too long, it sends back the next question but with a message that they took too long. This allows you to preserve the functionality of back/forward buttons in the browser.

You could do this in Javascript using setTimeout();

var t=setTimeout("killPage",30*1000);
function killPage(){
    window.location='/fail.php';
}
function questionComplete(){
    t.clearTimeout();
}

Try this:

php header('Refresh: 10'); 

It works for me maybe it will works for you.

You can try this function. It counts to your selected number and then restarts when reaches limit:

function timer($limit){
    if(!isset($_SESSION['start'])){
    $_SESSION['start']=time();
    }else if(isset($_SESSION['start'])){
    $timeleft=time()-$_SESSION['start'];
    }
    if($timeleft>=$limit){
    $_SESSION['start']=time();
    }
return $timeleft;
}
echo timer(5); //Counts to 5 and then restarts
 $timeLimit = 30; //seconds  

 $start = date("Y-m-d H:i:s",now());
 $end = date("Y-m-d H:i:s",now()+$timeLimit);

 for($x=0;$x<=$timeLimit;$x++){
    $next = date("Y-m-d H:i:s",now());
    $dif = getSecDifference($end,$next);            
    if($dif > 0)
        $x--;
    else{
        break;              
    }
}

///php will continue on to some action
///WRITE CODE HERE OR
////REDIRECT


function getSecDifference($to,$from)
{

    $date1 = new DateTime($to);
    $date2 = new DateTime($from);
    $interval = $date1->diff($date2);

    return number_format($interval->s,0,'','');


}

Although a timer can be used in PHP it's probably not the best solution here.

Your best bet would be to use Javascript setTimeout function (which runs a function when the time out has expired) on page load and then disable the time out if the submit button is pressed.

See http://www.w3schools./js/js_timing.asp

I think you'll want to do this in javascript, because if you use php sleep(30), the user wont be seeing anything until after 30 seconds.

The page will just keep loading.

What you can do is use the javascript function:

setTimeout('window.location = "tooslow.php"',30000);

Delay execution :

<?php sleep(30);
header("location: tooslow.php"); ?>

PHP

You can add meta tag <meta http-equiv="refresh" content="30"> to the header of your page, so the page will be refreshed after 30 seconds without javascript. But you still need to have some php logic to deal with sessions or cookies so you know that it is the user whose time run out.

发布评论

评论列表(0)

  1. 暂无评论