温馨提示:本文翻译自stackoverflow.com,查看原文请点击:javascript - Refresh part of my php page with setInterval()

javascript - 使用setInterval()刷新我的php页面的一部分

发布于 2020-03-27 15:50:30

我的PHP页面上有一个值,我想用每秒刷新一次setInterval()

因此,我实际上知道如何使用html等刷新值。但是现在,我想对php值执行相同的操作。这是我的代码:

<script>
setInterval(function()
    {

<?php
$urlMachineOnline = 'http://192.168.0.150/awp/Shredder/PLCfiles/MachineOnline.html';

// get content
$contentMachineOnline = file_get_contents($urlMachineOnline);

//remove first 2 characters
$truncateMachineOnline = substr($contentMachineOnline, 2);

//remove last 5 characters
$MachineActivityMS = substr($truncateMachineOnline, 0, -5);

//Set the value to seconds
$MachineActivityS = floor($MachineActivityMS /1000);

$formatMachineActive = 'H:i:s'; 

$TimeMachineActive = gmdate($formatMachineActive, $MachineActivityS);

?>

},1000);

</script>

因为JS和php arent真的很棒,所以Ofc不能正常工作。在我的桌子上,我只是简单地有:

<table>
    <tr>
        <td>Activity:</td>
        <td><p id='MachineActivity'></p><?php echo $TimeMachineActive; ?></td>
    </tr>
</table>

所以现在的问题是,当我按f5键时才刷新。但是现在我要自动刷新。我知道setInterval()为html工作。是否有可能为php代码完成此操作?

查看更多

查看更多

提问者
Bart
被浏览
31
Hammad 2015-03-18 20:58

这应该为您工作:

JS代码:

<script>
setInterval(function()
{

    $.ajax({

    url: 'value-generation.php',
    type: 'get',
    success: function(response){

    $("#MachineActivity").html(response)
    },

    });

        },1000);

    </script>

value-generation.php代码:

<?php
$urlMachineOnline = 'http://192.168.0.150/awp/Shredder/PLCfiles/MachineOnline.html';

// get content
$contentMachineOnline = file_get_contents($urlMachineOnline);

//remove first 2 characters
$truncateMachineOnline = substr($contentMachineOnline, 2);

//remove last 5 characters
$MachineActivityMS = substr($truncateMachineOnline, 0, -5);

//Set the value to seconds
$MachineActivityS = floor($MachineActivityMS /1000);

$formatMachineActive = 'H:i:s'; 

$TimeMachineActive = gmdate($formatMachineActive, $MachineActivityS);

echo $TimeMachineActive;

?>

发布
问题

分享
好友

手机
浏览

扫码手机浏览