PC前端JS实现
<script>
layui.use(['layer'], function () {
var $ = layui.jquery;
var layer = layui.layer;
//长轮询实现
var updateTimer = setInterval(() => {
var videoId = window.btoa('<?=$video->id;?>');
$.post('<?=route("member.xxx.time");?>', {video_id: videoId}, function (res) {
if (res.code === 2023) {
clearInterval(updateTimer);//出现想要的结果,停止重复提交
layer.msg(res.msg, {icon: 1, time: 3000}, function () {
parent.location.reload();
});
} else {
clearInterval(updateTimer);//出现想要的结果,停止重复提交
layer.msg(res.msg, {icon: 2, time: 3000}, function () {
parent.location.reload();
});
}
}, 'json');
}, 1000 * 60) //每分钟请求一次
});
</script>
后端PHP实现控制
....
//让前端停止再请求
if ($longTime == $learnedTime) {
throw new \Exception('恭喜您,通过实践',2023);
}