douminfu8033 2015-03-18 10:44
浏览 121
已采纳

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

I have a value on my PHP page and I want to refresh it per second with setInterval().

So I actually know how to refresh values with html etc. But now I want to do the same with php values. Here is my code:

<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>

Ofc this isn't working since JS and php arent really great together. and in my table I just simply have:

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

So the problem now is, it's only refreshing when I press f5. But now I want the autorefresh. I know setInterval() worked for html. Is it possible to get this done for php code?

  • 写回答

5条回答 默认 最新

  • dougu2006 2015-03-18 11:02
    关注

    This should work for you:

    JS Code:

    <script>
    setInterval(function()
    {
    
        $.ajax({
    
        url: 'value-generation.php',
        type: 'get',
        success: function(response){
    
        $("#MachineActivity").html(response)
        },
    
        });
    
            },1000);
    
        </script>
    

    value-generation.php code:

    <?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;
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大