比如
执行a代码如果在5秒钟内没有完成
就自动中断执行a代码,转向执行b代码
有人说用set_time_limit这个函数是可以超时显示错误信息
但我要的是超时转向其他操作
而不是输出错误信息请各位帮帮忙!

解决方案 »

  1.   

    试试这段代码$es = ini_get('error_reporting');
    register_shutdown_function( "time_out_callback");
    set_time_limit( 3 );//假设3秒超时
    error_reporting( 0 );//屏蔽fatal error
    //模拟超时
    while (true) {
    //echo connection_status()."<br/>";
    }
    error_reporting($es);//恢复设置function time_out_callback()
    {
    if(connection_status() == 2)
    {
    b();
    }
    }
    function b()
    {
    echo 'hello world!';
    }
      

  2.   

    不行啊
    你把while循环里那个注释去掉
    可以看到连接状态一直是0(CONNECTION_NORMAL)
    一执行里面就跳不出来了
      

  3.   

    为什么一定要去掉注释呢?我while里头注释那个东西没用的.
      

  4.   

    为了清晰一点$es = ini_get('error_reporting');
    register_shutdown_function( "time_out_callback");
    set_time_limit( 3 );//假设3秒就算超时
    error_reporting( 0 );//屏蔽fatal error
    //模拟超时
    c();
    error_reporting($es);//恢复设置function time_out_callback()
    {
    if(connection_status() == 2)
    {
    b();
    }
    }
    function c()
    {
    //你的超时代码
    while (true) {

    }
    }
    function b()
    {
    echo 'hello world!';
    }
      

  5.   

    晕。。while里执行不执行操作跟这个有什么关系,你在while(true)里echo ,每隔几毫秒就执行一次,会挤爆cpu的,while(true){}代码块相当于你的超时代码
    我执行这段东西是3个0,一个2,你呢?
    $es = ini_get('error_reporting');
    register_shutdown_function( "time_out_callback");
    set_time_limit( 3 );//假设3秒就算超时
    error_reporting( 0 );//屏蔽fatal error
    //模拟超时
    c();
    error_reporting($es);//恢复设置function time_out_callback()
    {
    if(connection_status() == 2)
    {
    b();
    }
    }
    function c()
    {
    //你的超时代码
    while (true) {

    echo connection_status()."<br/>";//echo 0
    sleep( 1 );//给cpu透口气

    }
    }
    function b()
    {
    echo connection_status();//echo 2,c超时
    }
      

  6.   

    不过这样time out的callback执行时间也不能超时.
      

  7.   

    那里不会啊
    那里面我给他 ECHO 返回错误信息之类的
      

  8.   

    用什么抓的,我都是用wget, 本身可以设timeout时间
      

  9.   

    手册上看到这个:http://ca2.php.net/manual/en/function.file-get-contents.phpSetting the timeout properly without messing with ini values: <?php 
    $ctx = stream_context_create(array( 
        'http' => array( 
            'timeout' => 1 
            ) 
        ) 
    ); 
    file_get_contents("http://example.com/", 0, $ctx); 
    ?>