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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器