問題描述
是否可以僅對命令或僅對函數設置時間限制,例如:
Hi is there a possibility to set time limit only to a command or only to a function eg:
function doSomething()
{
//..code here..
function1();
//.. some code here..
}
我只想為function1設置時間限制.
I want to set time limit only to function1.
有退出 set_time_limit 但我認為這設置了整個腳本的時間限制.有人有什么想法嗎?
There exits set_time_limit but I think this sets the time limit to whole script. Anybody any Idea?
推薦答案
set_time_limit() 確實在全局運行,但可以在本地重置.
set_time_limit() does run globally, but it can be reset locally.
設置允許腳本運行的秒數.如果達到了,該腳本返回一個致命錯誤.默認限制為 30 秒,或者,如果存在,php.ini 中定義的 max_execution_time 值.
Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.
調用時,set_time_limit(
) 從零重新啟動超時計數器.在換句話說,如果超時是默認的 30 秒,和 25 秒進入腳本執行調用如 set_time_limit(20) 時,腳本將在超時前總共運行 45 秒.
When called, set_time_limit(
) restarts the timeout counter from zero. In
other words, if the timeout is the default 30 seconds, and 25 seconds
into script execution a call such as set_time_limit(20) is made, the script
will run for a total of 45 seconds before timing out.
我沒有測試過,但你可以在本地設置,離開時重置
I've not tested it, but you may be able to set it locally, resetting when you leave the
<?php
set_time_limit(0); // global setting
function doStuff()
{
set_time_limit(10); // limit this function
// stuff
set_time_limit(10); // give ourselves another 10 seconds if we want
// stuff
set_time_limit(0); // the rest of the file can run forever
}
// ....
sleep(900);
// ....
doStuff(); // only has 10 secs to run
// ....
sleep(900);
// ....
set_time_limit
()... 在確定腳本運行的最長時間時,不包括在腳本執行之外發生的活動上花費的任何時間,例如使用 system() 的系統調用、流操作、數據庫查詢等.在測量時間是真實的 Windows 上,情況并非如此.
這篇關于限制函數或命令 PHP 的執行時間的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!