项目需求:
数据可视化大屏开发中,需要设置定时任务:2点时返回10人,5点时返回20人……实现自动刷新,不能手动刷新页面。
项目理解:javascript可以设置定时任务,但是无法执行任务; php也无法执行任务;
解决方案:前端ajax轮询刷新php页面data.php;
function getContent() {
$.ajax({
type: "post",
async: true,
url: "data.php",
data: {},
dataType: "text",
success: function (res) {
if (res) {
$("#content").html(res);
}
},
error: function (err) {
console.log("Ajax err:" + err);
}
});
}
//定时刷新数据;
var timerAll;
getContent();
clearInterval(timerAll);
timerAll = setInterval(function () {
getContent();
}, 10 * 1000);
data.php设置时间条件
function getInfo()
{
$time1 = strtotime(date('Y-m-d 1:39:00'));
$time2 = strtotime(date('Y-m-d 1:40:00'));
$time3 = strtotime(date('Y-m-d 1:41:00'));
$now = strtotime(date('Y-m-d H:i:s'));
if ($now >= $time1 && $now = $time2 && $now = $time3) {
return $time3;
}
}
echo getInfo();
Done!