/*
* @名称: 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()的更多相关文章

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

    上周要做一个活动页面 秒杀列表页 需要一个时间的算法排序 自己琢磨了半天想了各种算法也没搞出来,后来问了下一个后台的php同学 他写了个算法给我看了下 ,刚开始看的时候觉得这就是个纯算法,不能转化成页 ...

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

    总结以下三种方法,实现c#每隔一段时间执行代码: 方法一:调用线程执行方法,在方法中实现死循环,每个循环Sleep设定时间: 方法二:使用System.Timers.Timer类: 方法三:使用Sys ...

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

    此处是转载的  是给自己做的备注 问题:隔一段时间应用就会挂掉(进程在,但停止响应,也无log输出),必须重启tomcat 原因查找:由于tomcat自身log中并无错误产生,磁盘空间足够,读写也正常 ...

  4. 计数排序(O(n+k)的排序算法,空间换时间)

    计数排序就是利用空间换时间,时间复杂度O(n+k) n是元素个数,k是最大数的个数: 统计每个数比他小的有多少,比如比a[i]小的有x个,那么a[i]应该排在x+1的位置 代码: /* * @Auth ...

  5. logback 指定每隔一段时间创建一个日志文件

    我使用的logback版本是1.2.3 目前logback支持根据时间来配置产生日志文件,但是只支持每周,每天,每个小时,每分钟等创建一个文件,配置如下: <appender name=&quo ...

  6. TLS 改变密码标准协议(Change Cipher Spec Protocol) 就是加密传输中每隔一段时间必须改变其加解密参数的协议

    SSL修改密文协议的设计目的是为了保障SSL传输过程的安全性,因为SSL协议要求客户端或服务器端每隔一段时间必须改变其加解密参数.当某一方要改变其加解密参数时,就发送一个简单的消息通知对方下一个要传送 ...

  7. js setInterval每隔一段时间执行一次

    js setInterval每隔一段时间执行一次setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式.setInterval() 方法会不停地调用函数,直到 clearI ...

  8. python每隔一段时间做一个事情

    #!/usr/bin/env python #coding:utf8 #Author:lsp #Date:下午2:17:54 #Version:0.1 #Function: 每隔一段时间做一个事情 f ...

  9. 纯JS实现鼠标每隔一段时间才能让页面再次滚动

    这里没有用到浏览器的兼容性写法,只是提供思路(这里使用的是Google浏览器的方法) javascript代码部分: //获取html元素var oHtml =document.documentEle ...

随机推荐

  1. [书]WALL·E、龙与地下铁、中国美丽的故事、故事新编、四十自述、书虫、人工智能、大话数据结构

    下午有时间,逛了逛了书城,看到了一些书.在这里总结一些自己的感受.   一.<龙与地下铁>     这本书是我首先看到的,就在靠前的新书区.是小说,我没看里面的内容,但是被书封皮的宣传文案 ...

  2. spring cache

    spring-ehcache.xml文件内容如下: <?xml version="1.0" encoding="UTF-8"?> <beans ...

  3. python os模块(1)

    os模块主要处理文件和目录(文件夹)的创建.删除.检查判定.属性值修改.路径修改. (1)获取当前目录的两种方法 1 os.getcwd() os.path.abspath('.') (2)创建文件夹 ...

  4. Python namedtuple

    我们都知道Python中的tuple是一个非常高效的集合对象,但是我们只能通过索引的方式访问这个集合中的元素,比如下面的代码: Bob=('bob',30,'male') print'Represen ...

  5. 一个java的DES加密解密类转换成C#

    一个java的des加密解密代码如下: //package com.visionsky.util; import java.security.*; //import java.util.regex.P ...

  6. jquery总结05-常用事件04-委托事件

    委托事件on 多个事件绑定同一个函数 $("#elem").on("mouseover mouseout",function(){ });通过空格分离,传递不同 ...

  7. 配置org.springframework.scheduling.quartz.CronTriggerBean (转载)

    在项目中又用到了定时器,对于定时器的应用总是模模糊糊的,今天结合网上找到的资料与自己在项目中写的简单地在此写一下,以备需要时查阅. 一个Quartz的CronTrigger表达式分为七项子表达式,其中 ...

  8. iOS Question

    Q1: dyld: Library not loaded: @rpath/libswiftCore.dylib 1. 退出 Xcode2. 重启电脑3. 找到 这个 DerivedData 文件夹 删 ...

  9. Java原始的8中数据类型

    数据类型 大小 范围 默认值 ========   ========  ============================================  =========byte(字节) ...

  10. Deep Learning 4_深度学习UFLDL教程:PCA in 2D_Exercise(斯坦福大学深度学习教程)

    前言 本节练习的主要内容:PCA,PCA Whitening以及ZCA Whitening在2D数据上的使用,2D的数据集是45个数据点,每个数据点是2维的.要注意区别比较二维数据与二维图像的不同,特 ...