Javascript PHP 计时器等待 30 秒然后运行命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8942610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Timer wait 30 seconds then run a command
提问by Brett Stirling
I'm just looking for a simple timer, where I can get my page to run a script after 30 seconds.
我只是在寻找一个简单的计时器,在那里我可以让我的页面在 30 秒后运行脚本。
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.
这个想法是用户有 30 秒的时间来提交答案,否则页面将运行脚本并将他们带到“抱歉,太慢”样式的页面。
I cannot find the correct php function for this, but it basically we be like:
我找不到正确的 php 函数,但基本上我们是这样的:
<?php
Start timer(30);
when timer runs out:
header("location: tooslow.php");
?>
Thanks for any help, Brett
感谢您的帮助,布雷特
回答by Polynomial
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.
将页面请求时间(来自time()
函数)存储在会话变量中,然后在用户发布答案时从提交时间(同样来自同一个函数)中减去请求时间。如果差值大于 30,则它们花费的时间太长。
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.
当然,如果您希望自动跳转到页面,则可以将上述方法与此处其他答案中的JavaScript方法之一结合使用。但是,仅 JavaScript 就可以绝对零保证用户在时限内解决问题,因为您可以禁用 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 completes 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.
我个人的建议是您根本不使用页面重定向,而是使用 Ajax 来提供交互式界面。本质上,您setTimeout
像往常一样使用调用,但是当它完成时,它会向用 PHP 编写的检查脚本发出 Ajax 请求。该脚本将实现我上面提供的会话计时逻辑。如果时间合适,脚本会在他们答对时增加他们的分数,然后回答下一个问题,该问题可以显示在页面上。如果时间太长,它会发回下一个问题,但会显示他们花了太长时间的消息。这允许您保留浏览器中后退/前进按钮的功能。
回答by Paul Bain
You could do this in Javascript using setTimeout();
您可以使用 setTimeout() 在 Javascript 中执行此操作;
var t=setTimeout("killPage",30*1000);
function killPage(){
window.location='/fail.php';
}
function questionComplete(){
t.clearTimeout();
}
回答by Raffy Beloy
Try this:
尝试这个:
php header('Refresh: 10');
It works for me maybe it will works for you.
它对我有用,也许对你有用。
回答by user2483110
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
回答by CG_DEV
$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,'','');
}
回答by Andy Pieters
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.
我想你会想在 javascript 中做到这一点,因为如果你使用 php sleep(30)
,用户在 30 秒后才会看到任何东西。
The page will just keep loading.
该页面将继续加载。
What you can do is use the javascript function:
你可以做的是使用 javascript 函数:
setTimeout('window.location = "tooslow.php"',30000);
回答by celionor
回答by JConstantine
Although a timer can be used in PHP it's probably not the best solution here.
尽管可以在 PHP 中使用计时器,但在这里可能不是最佳解决方案。
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.
您最好的选择是在页面加载时使用 Javascript setTimeout 函数(它在超时到期时运行一个函数),然后在按下提交按钮时禁用超时。
回答by Sergiy T.
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.
您可以将元标记添加<meta http-equiv="refresh" content="30">
到页面的标题中,因此页面将在 30 秒后在没有 javascript 的情况下刷新。但是您仍然需要一些 php 逻辑来处理会话或 cookie,以便您知道是用户的时间用完了。