zoukankan      html  css  js  c++  java
  • php轮流排序,每隔一定的时间轮流进行位置排序,轮询的排行榜:function dataPollingInterval()

    /*
    * @名称: php ,对数组每隔一定的时间(自设定时间)来轮流进行位置排序,轮询的排行榜。
                    精确到指定的秒 或 分钟 或 小时 或 天 ,对数据列表进行轮排。
    * @参数: (array)$list 顺序数组,传入需要进行轮排的数组;
    * @参数: (time string)$polling_time  间隔时间 , 轮排间隔的时间。可以是:数字 + 秒、分、时、天(英文单词);
    * @参数: (int)$polling_number 每次轮流换排多少条数据;
    * @返回值: array | false , 如果排序成功返回array,否则异常情况返回false.
    *
    * @author: 王奇疏 , QQ: 876635409
    */
    function dataPollingInterval( $list ,  $polling_time='10 second minute hour day' , $polling_number=1 ) {
        // 规划轮询间隔时间的参数:
        $interval = false;
    
        // 判断$polling_time 的类型是 秒、分、小时、天 中的哪1种。
        $arg = array( 
            's'=>1  ,            //
            'm'=>60 ,            // 分= 60 sec
            'h' =>3600 ,        // 时= 3600 sec
            'd' => 86400 ,    // 天= 86400 sec
        );
    
        // 判断间隔时间的类型,并计算间隔时间
        foreach ( $arg as $k => $v ) {
            if ( false !== stripos( $polling_time , $k ) ) {
                $interval = intval( $polling_time ) * $v;
                break;
            }
        }
        
        // 判断间隔时间
        if( !is_int( $interval ) ){  
            return false;
        }
    
        // 从今年开始的秒数
        $this_year_begin_second = strtotime( date( 'Y-01-01 01:00:01' , time() ) );
        
        // 当前秒数 - 今年开始的秒数,得到今年到目前为止的秒数。
        $polling_time = time() - $this_year_begin_second;
    
        // 从今年到目前为止的秒数,计算得到当前轮数
        $len = count( $list ); // 总长度
        $start_index = intval( $polling_time / $interval );
        $start_index = $polling_number * $start_index  % $len; // 轮排数量 * 轮数 , 取余 总数量。
    
        $res = array(  );
    
        // 将轮数 指向到数组的索引,然后从这个索引开始接着往下循环遍历。
        for ( $i=0; $i < $len ; ++$i ) {
            $index = $i + $start_index; // 索引的变化是根据时间来变
            
            // 当遍历索引超过数组的最大下标时, 
            if ( $index >= $len ) {
                $index = $index - $len ;
            }
            $res[] = $list[ $index ]; // 存入结果
        }
        return $res;
    }    

    数据处理例子:
    --------------------------------------------
     43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

     63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

     83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1 2

     3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

     23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

    -------------------------------------------
    下一秒

     45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

     65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

     85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1 2 3 4

     5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

     25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    ==============================

    【完整例子】:

    <?php
        date_default_timezone_set( 'prc' );
        if ( isset( $_GET['go'] ) ) {
            // 数组数据:
            $list = range( 1 , 100 ); 
            
            // 按时间轮流排序:对list列表每5秒进行一次位置轮询排序,每次排10条。
            $list = dataPollingInterval( $list , '2 sec' , 3 ) ;
            
            // 输出排序好的数据
            $out = '';
            foreach ( $list as $k=>$v ) {
                $out .= ' '.$v;
                if ( $k % 20 == 19 ) {
                    $out .= '<br /><br />';
                }
            }
            echo '<pre>';print_r( $out );echo '</pre>';
            exit;
        }
        
    
        /*
        * @名称: 对数组每隔一定的时间(自设定时间)来轮流进行位置排序,轮询的排行榜。
                        精确到指定的秒 或 分钟 或 小时 或 天 ,对数据列表进行轮排。
        * @参数: (array)$list 顺序数组,传入需要进行轮排的数组;
        * @参数: (time string)$polling_time  间隔时间 , 轮排间隔的时间。可以是:数字 + 秒、分、时、天(英文单词);
        * @参数: (int)$polling_number 每次轮流换排多少条数据;
        * @返回值: array | false , 如果排序成功返回array,否则异常情况返回false.
        *
        * @author: 王奇疏 , QQ: 876635409
        */
        function dataPollingInterval( $list ,  $polling_time='10 second minute hour day' , $polling_number=1 ) {
            // 规划轮询间隔时间的参数:
            $interval = false;
    
            // 判断$polling_time 的类型是 秒、分、小时、天 中的哪1种。
            $arg = array( 
                's'=>1  ,            //
                'm'=>60 ,            // 分= 60 sec
                'h' =>3600 ,        // 时= 3600 sec
                'd' => 86400 ,    // 天= 86400 sec
            );
    
            // 判断间隔时间的类型,并计算间隔时间
            foreach ( $arg as $k => $v ) {
                if ( false !== stripos( $polling_time , $k ) ) {
                    $interval = intval( $polling_time ) * $v;
                    break;
                }
            }
            
            // 判断间隔时间
            if( !is_int( $interval ) ){  
                return false;
            }
    
            // 从今年开始的秒数
            $this_year_begin_second = strtotime( date( 'Y-01-01 01:00:01' , time() ) );
            
            // 当前秒数 - 今年开始的秒数,得到今年到目前为止的秒数。
            $polling_time = time() - $this_year_begin_second;
    
            // 从今年到目前为止的秒数,计算得到当前轮数
            $len = count( $list ); // 总长度
            $start_index = intval( $polling_time / $interval );
            $start_index = $polling_number * $start_index  % $len; // 轮排数量 * 轮数 , 取余 总数量。
    
            $res = array(  );
    
            // 将轮数 指向到数组的索引,然后从这个索引开始接着往下循环遍历。
            for ( $i=0; $i < $len ; ++$i ) {
                $index = $i + $start_index; // 索引的变化是根据时间来变
                
                // 当遍历索引超过数组的最大下标时, 
                if ( $index >= $len ) {
                    $index = $index - $len ;
                }
                $res[] = $list[ $index ]; // 存入结果
            }
            return $res;
        }    
    
    ?>
    
    <!DOCTYPE>
    <html>
    <head>
        <title>php轮流排序</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    
    <body>
    
        <div id="containner" class=""></div>
    
    </body>
    </html>
    
    <script type="text/javascript">
    <!--
        var $ = function ( id ) {
            return typeof id == "string" ? document.getElementById( id ) : id;
        }
    
        // ajax方法:ajax(  url , function(){ ... } , if_post_param );
        function ajax(B,A){this.bindFunction=function(E,D){return function(){return E.apply(D,[D])}};this.stateChange=function(D){if(this.request.readyState==4){this.callbackFunction(this.request.responseText)}};this.getRequest=function(){if(window.ActiveXObject){return new ActiveXObject("Microsoft.XMLHTTP")}else{if(window.XMLHttpRequest){return new XMLHttpRequest()}}return false};this.postBody=(arguments[2]||"");this.callbackFunction=A;this.url=B;this.request=this.getRequest();if(this.request){var C=this.request;C.onreadystatechange=this.bindFunction(this.stateChange,this);if(this.postBody!==""){C.open("POST",B,true);C.setRequestHeader("X-Requested-With","XMLHttpRequest");C.setRequestHeader("Content-type","application/x-www-form-urlencoded");C.setRequestHeader("Connection","close")}else{C.open("GET",B,true)}C.send(this.postBody)}};
    
    
        window.setInterval( 
            function (  ) {
                ajax( 
                    '?go=1' ,
                    function ( text ) {
                        $( "containner" ).innerHTML = text;
                    }
                );
            } ,
            1000
        );
    //-->
    </script>
  • 相关阅读:
    hive实战记录
    并发
    软件安装
    idea
    maven
    thingkinginjava
    JVM
    并发模块concurrent
    ffmpeg_分割一个mp4文件到多个小的mp4文件
    用 ffmpeg 压缩视频
  • 原文地址:https://www.cnblogs.com/wangqishu/p/3825688.html
Copyright © 2011-2022 走看看