JS无法刷新

问题描述 投票:0回答:1

我有一个名为angularGaugeChart.php的php文件。它将使用“ plot.ly”库显示一个图表。第一次通过浏览器访问它时,它可以正确显示图表。我要自动刷新。因此,我添加了以下脚本。 php可以自动刷新,但刷新后无法显示图表。我想知道问题出在哪里?

<html>
<head></head>
<body>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>

<div id="solarRad" style="width:500px;height:400px;">
<?php 
include 'SQL.php';

mysql_select_db($dbname);

$sqlSolarRad="SELECT * FROM `Test` order by date DESC, time DESC limit 1 ";
$restsqlSolarRad=mysql_query($sqlSolarRad);
while($rowsSqlSolarRad=mysql_fetch_array($restsqlSolarRad))
{
$lastRecordSolarRad= $rowsSqlSolarRad['solarRad'];
}

?>  

<script>
var dataSolarRad = [
  {
    domain: { x: [0, 1], y: [0, 1] },
    value: <?php echo $lastRecordSolarRad ?>,
    title: { text: "Solar Radiation (W/m²)" },
    type: "indicator",
    mode: "gauge+number",
    delta: { reference: 400 },
    gauge: { axis: { range: [null, 1366] } }
  }
];

var layout = { width: 700, height: 400};
Plotly.newPlot('solarRad', dataSolarRad, layout);
</script>
</div>


<script src="jquery-3.4.1.min.js"></script>
<script type="text/javascript">
    document.onload = setupRefresh();
    function setupRefresh()
    {
        setInterval("refreshBlock();",10000);
    }

    function refreshBlock()
    {
       $("#solarRad").load("angularGaugeChart.php #solarRad");
    }
</script>

</body>
</html>
javascript php jquery
1个回答
0
投票
  1. [document.onload = setupRefresh();是错误的,应该是document.onload = setupRefresh;

  2. 您使用jQuery,所以使用它!!!也破坏缓存


$(function() {
  setInterval(function () {
    $("#solarRad").load("angularGaugeChart.php?rdn="+new Date().getTime()+" #solarRad");
  },10000) 
})
© www.soinside.com 2019 - 2024. All rights reserved.