您当前的位置: 首页 >  php

漏刻有时

暂无认证

  • 0浏览

    0关注

    717博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

ajax刷新php后台实现定时任务的执行解决方案

漏刻有时 发布时间:2020-11-04 01:47:39 ,浏览量:0

项目需求:

数据可视化大屏开发中,需要设置定时任务: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!

关注
打赏
1661217259
查看更多评论
立即登录/注册

微信扫码登录

0.0397s