每x秒自动刷新一次Html表


Auto Refresh a Html table every x seconds

我正在尝试刷新我的表,因为其中的变量不断更新,我想每隔几秒钟重新更新一次这些变量。我已经完成了我的代码,给表一个id并为它创建一个div。代码将解释我实际做了什么。提前感谢已编辑:添加了变量表和变量刷新器。然而,代码仍然无法重新加载我的表!有什么想法吗

测试.php

       <html>
<head>
    <script type='text/javascript'>
 var table = $('#tableID');
// refresh every 5 seconds
var refresher = setInterval(function() {
  table.load("1103242B/processing/js.php");
}, 5000);
setTimeout(function() {
  clearTimeout(refresher);
}, 1800000);
</script>
</head>
<body>
<?php include_once'js.php'; ?>
</body>
</html>

JS.php

<?php
require_once 'connect.php';
include 'start.php';
include 'functions.php';
header("Cache-Control: no-cache,no-store");
$query = "SELECT * FROM opentrades"; //You don't need a ; like you do in SQL
$result = mysql_query($query);
echo "<table border = '1px' id='tableID'>"; // start a table tag in the HTML
echo "<tr><td>" . "Order Number" . "</td><td>" . "Selection" . "</td><td>" . "Date" . "</td><td>" . "Type" . "</td><td>" . "Size" . "</td><td>" . "Bid Price" . "</td><td>" . "Offer Price" . "</td><td>" ."Stop Loss" . "</td><td>" . "Take Profit" . "</td><td>" ."Profit/Loss(USD)"."</td><td>" ."Close"."</td></tr>" ;  //$row['index'] the index here is a field name
while($row = mysql_fetch_assoc($result)){   //Creates a loop to loop through results
if ($row['selection']=='eur/usd')// TO RETRIEVE BID AND OFFER FOR EACH ROW
      {
      $bidpricepl=$bid;
      $offerpricepl=$bid1;
      }
  elseif ($row['selection']=='usd/jpy')
      {
      $bidpricepl=$bid2;
      $offerpricepl=$bid3;
      }
  elseif ($row['selection']=='usd/cad')
      {
      $bidpricepl=$bid4;
      $offerpricepl=$bid5;
      }
  elseif ($row['selection']=='eur/jpy')
      {
      $bidpricepl=$bid6;
      $offerpricepl=$bid7;
      }
  elseif ($row['selection']=='eur/chf')
      {
      $bidpricepl=$bid8;
      $offerpricepl=$bid9;
      }
  elseif ($row['selection']=='gbp/usd')
      {
      $bidpricepl=$bid10;
      $offerpricepl=$bid11;
      }
  elseif ($row['selection']=='aud/usd')
      {
      $bidpricepl=$bid12;
      $offerpricepl=$bid13;
      }
  elseif ($row['selection']=='usd/chf')
      {
      $bidpricepl=$bid14;
      $offerpricepl=$bid15;
      }
  if ($row['type']=="buy")
    {
      $minipipskiller='10';
      $offeropen=$row['offerprice'];
      $pips=$offerpricepl-$offeropen;
      $closedb=$offeropen;
      $pips1=round($pips, 6);
      $pips2 = str_replace('.', '', $pips1);
        if ($pips2<0)
      {
        $pips2 = str_replace('-', '', $pips2);
        $pips2 = ltrim($pips2, '0');
        $pips2 = -1 * abs($pips2);
      }
      else {
        $pips2 = ltrim($pips2, '0');
      }
      $pips3=$pips2/$minipipskiller;
    }// PIP COUNTING
  elseif ($row['type']=="sell")//FOR PIP COUNTING
    {
      $minipipskiller='10';
      $bidopen=$row['bidprice'];
      $pips=$bidopen-$bidpricepl;
      $closedb=$bidopen;
      $pips1=round($pips, 6);
      $pips2 = str_replace('.', '', $pips1);
      if ($pips2<0)
      {
        $pips2 = str_replace('-', '', $pips2);
        $pips2 = ltrim($pips2, '0');
        $pips2 = -1 * abs($pips2);
      }
      else {
        $pips2 = ltrim($pips2, '0');
      }
      $pips3=$pips2/$minipipskiller;
    }

      $ticksize= "0.0001";// FOR PROFIT AND LOSS
      $lot1 = "100000";
      $sizecalc=$row['size'] * $lot1;
        if ($row['type']=="buy")
      { 
        $profitandloss=$sizecalc*$ticksize*$pips3; //per TRADE
      }
      if ($row['type']=="sell")
      {
        $profitandloss=$sizecalc*$ticksize*$pips3; //per TRADE
      }
      $zero= '0';
      if($profitandloss<$zero) {
            $profitText = "<div style='"color: red;'">$profitandloss</div>";
        } elseif ($profitandloss>$zero) {
            $profitText = "<div style='"color: green;'">$profitandloss</div>";
        }

$sum+= $profitandloss;

    echo "<tr><td>" . $row['trade_id'] .         
        "</td><td>" . $row['selection'] . 
        "</td><td>" . $row['date'] .
        "</td><td>" . $row['type'] .
        "</td><td>" . $row['size'] .
        "</td><td>" . $row['bidprice'] .
        "</td><td>" . $row['offerprice'] .
        "</td><td>" . $row['stoploss'] .
        "</td><td>" . $row['takeprofit'] .
        "</td><td>" . $profitText . 
        "</td><td><a href ='delete.php?id=".
        $row['trade_id']."'>X</a>
       </td></tr>";  
$profitandloss=0;
if($sum<$zero) {
    $sumText = "<div style='"color: red;'">$sum</div>";
} elseif ($sum>$zero) {
    $sumText = "<div style='"color: green;'">$sum</div>";
}
}
echo "</table><br>";

?>

我认为setIntervaljQuery.load是您正在寻找的

var table = $("#tableID");
// refresh every 5 seconds
var refresher = setInterval(function() {
  table.load("/path/to/js.php");
}, 5000);

或者用缩短

var refresher = setInterval(table.load.bind(table, "/path/to/data"), 5000);

如果你想停止刷新数据,(例如)说用户长时间打开页面

// stop refreshing after 30 minutes
setTimeout(function() {
  clearTimeout(refresher);
}, 1800000);

如果数据加载需要一段时间,则您可能希望在加载数据后仅刷新X秒。你可以使用setTimeout 这样做

var table = $("#tableID");
var refresh = function() {
  table.load("/path/to/js.php", function() {
    setTimeout(refresh, 5000);
  });
};
refresh();

只有我的2美分,但这似乎是一个不必要的负载。我会考虑像触发器这样的东西,并写信给像这样价格较低的东西

bool = timestamp > now;
  or
if(myhash != tablehash)

因此,您的长轮询只是询问是否发生了更改,而不是运行查询

更改

$('#tableID').replaceWith($(data)); 

进入

$('#tableID').replaceWith(data);

尽管你所做的事情是将一些东西加载到一个元素中。因此,从语义上讲,使用.load()听起来更好。

$('#tableID').load('some/url/file.php')