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>

php轮流排序,每隔一定的时间轮流进行位置排序,轮询的排行榜:function dataPollingInterval()

时间: 2024-08-01 18:45:02

php轮流排序,每隔一定的时间轮流进行位置排序,轮询的排行榜:function dataPollingInterval()的相关文章

javascript 一个关于时间排序的算法(一个页面多个倒计时排序)

上周要做一个活动页面 秒杀列表页 需要一个时间的算法排序 自己琢磨了半天想了各种算法也没搞出来,后来问了下一个后台的php同学 他写了个算法给我看了下 ,刚开始看的时候觉得这就是个纯算法,不能转化成页面的dom效果,可是再看了两遍发现可以 于是我就改了改 实现了 不禁感叹 确实蛮赞的 于是就博一客: 页面需求是:从11点到20点 每隔一个小时一场秒杀 如果是当前时间就显示正在秒杀 之前的商品就往最后排 以此类推 类似最开始的11点顺序是 11,12,13,14,15,16,17,18,19,20

setTimeout() 实现程序每隔一段时间自动执行

定义和用法 setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. 语法 setTimeout(code,millisec) 参数 描述 code 必需.要调用的函数后要执行的 JavaScript 代码串. millisec 必需.在执行代码前需等待的毫秒数. 提示和注释 提示:setTimeout() 只执行 code 一次.如果要多次调用,请使用 setInterval() 或者让 code 自身再次调用 setTimeout(). [示例] //每5秒使用Ajax取一次

c#实现每隔一段时间执行代码(多线程)

总结以下三种方法,实现c#每隔一段时间执行代码: 方法一:调用线程执行方法,在方法中实现死循环,每个循环Sleep设定时间: 方法二:使用System.Timers.Timer类: 方法三:使用System.Threading.Timer: 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 45 46

storm每隔一段时间发送tuple

TickTuple可以满足每隔一段时间发送tuple,"__system" component会定时往task发送 "__tick" stream的tuple, 发送频率由TOPOLOGY_TICK_TUPLE_FREQ_SECS来配置, 可以在default.ymal里面配置, 也可以在代码里面通过getComponentConfiguration()来进行配置. 代码里面配置如下 public Map<String, Object> getCompo

可以每隔一段时间自动运行的一个批处理

可以每隔一段时间自动运行的一个批处理  zidong.bat代码 @echo off :Begin Start "d:\print.bat" rem 下面这行是延时代码,300是秒做单位的,300秒就是5分钟,需要多长时间自改一下 @Ping 127.0.0.1 -n 300 Goto Begin 执行后有个问题是每次关不掉print.bat print.bat代码 net session /delete /y 问题有待解决

实现每次触发事件后隔一段时间后才能再次触发事件

比如: 每次点击“提交”按钮后隔3s后才能再次提交. 可以这样:点击后设置按钮不可用,并在3s后再设置按钮可用 $('#button1').click(function(){ //在这里提交或做别的事 $(this).attr("disabled",true); setTimeout("$('#button1').attr('disabled',false);", 3000); //此处setTimeout里面不能用$(this),this指的是window对象 }

隔一段时间应用就会挂掉(进程在,但停止响应,也无log输出),必须重启tomcat

此处是转载的  是给自己做的备注 问题:隔一段时间应用就会挂掉(进程在,但停止响应,也无log输出),必须重启tomcat 原因查找:由于tomcat自身log中并无错误产生,磁盘空间足够,读写也正常,排除其他原因后怀疑是jvm崩溃导致,于是查gc日志:从上图可以看出每次full GC是老年代内存占用量相当小,远没有达到它的最大值,持久代也一样,而且每次GC过后,占用量基本没有变化,也就是说,每一次full GC都是在做无用功,白白浪费掉了一大批性能.从内存占用率方面看不是内存分配不够,应该是程

setTimeout() 实现程序每隔一段时间自己主动运行

定义和使用方法 setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. 语法 setTimeout(code,millisec) 參数 描写叙述 code 必需.要调用的函数后要运行的 JavaScript 代码串. millisec 必需.在运行代码前需等待的毫秒数. 提示和凝视 提示:setTimeout() 仅仅运行 code 一次.假设要多次调用,请使用 setInterval() 或者让 code 自身再次调用 setTimeout(). [演示样例] //每5秒使用

睡排序--利用线程sleep的时间排序

/* * 睡排序 * 利用线程休眠(苏醒时间)将数排序 */public class Demo { public static void main(String[] args) { int[] sortNum = {-1,0,1,4,7,3,8,9,2,6,5,555}; SortThread[] sortThreads = new SortThread[sortNum.length]; for (int i = 0; i < sortThreads.length; i++) { //接收数字充