自动刷新一个HTML表格每x秒 [英] Auto Refresh a Html table every x seconds

查看:108
本文介绍了自动刷新一个HTML表格每x秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图刷新一个表,我有在那里不断地被更新的变量,我想重新更新这些变量每隔几秒钟。我已经做了code对我而言,通过给表中的ID和创建一个div它。在code将解释其实我。提前致谢 ! 编辑:增加了无功表和无功进修。然而code仍然不会重装我的表!任何想法??

test.php的

 < HTML>
< HEAD>
    <脚本类型=文/ JavaScript的'>
 无功表= $('#TABLEID');

//刷新每5秒
VAR复习=的setInterval(函数(){
  table.load(1103242B /处理/ js.php);
},5000);

的setTimeout(函数(){
  clearTimeout(进修);
},180万);
< / SCRIPT>
< /头>
<身体GT;
< PHP include_once'js.php; ?>

< /身体GT;
< / HTML>
 

JS.php

 < PHP
require_once'将connect.php;
包括start.php;
包括'的functions.php;
标题(缓存控制:无缓存,无店);

$查询=SELECT * FROM opentrades; //你不需要一个;就像你在SQL做
$结果= mysql_query($查询);
回声<表格边框='1px的'ID ='TABLEID'>中; //开始在HTML表格标记
回声< TR>< TD>中。 订单号 。 &所述; / TD>&其中; TD>中。 选择。 &所述; / TD>&其中; TD>中。 日期 。 &所述; / TD>&其中; TD>中。 类型 。 &所述; / TD>&其中; TD>中。 尺寸 。 &所述; / TD>&其中; TD>中。 竞价 。 &所述; / TD>&其中; TD>中。 提供的价钱 。 &所述; / TD>&其中; TD>中止损。 &所述; / TD>&其中; TD>中。 获利。 &所述; / TD>&其中; TD>中利润/亏损(美元)。< / TD>< TD>中关闭。< / TD>< / TR>中; // $行['指数']在这里指数是一个字段名

而($行= mysql_fetch_assoc($结果)){//创建通过结果的循环来
如果($行['选择'] =='欧元/美元)//检索买入价和卖出FOR EACH ROW
      {
      $ bidpricepl = $投标;

      $ offerpricepl = $标段;

      }
  ELSEIF($行['选择'] ==美元/日元)
      {
      $ bidpricepl = $标段2;

      $ offerpricepl = $标段3;

      }
  ELSEIF($行['选择'] =='USD / CAD)
      {
      $ bidpricepl = $ bid4;

      $ offerpricepl = $ bid5;

      }
  ELSEIF($行['选择'] =='欧元/日元)
      {
      $ bidpricepl = $ bid6;

      $ offerpricepl = $ bid7;

      }
  ELSEIF($行['选择'] =='欧元/瑞郎)
      {
      $ bidpricepl = $ bid8;

      $ offerpricepl = $ bid9;

      }
  ELSEIF($行['选择'] =='英镑/美元)
      {
      $ bidpricepl = $ bid10;

      $ offerpricepl = $ bid11;

      }
  ELSEIF($行['选择'] =='澳元/美元)
      {
      $ bidpricepl = $ bid12;

      $ offerpricepl = $ bid13;

      }
  ELSEIF($行['选择'] =='美元/瑞郎)
      {
      $ bidpricepl = $ bid14;

      $ offerpricepl = $ bid15;

      }
  如果($行['类型'] ==买入)
    {

      $ minipipskiller = '10';
      $ offeropen = $行['offerprice'];
      $点数= $ offerpricepl- $ offeropen;
      $ closedb = $ offeropen;
      $ pips1 = ROUND($个点,6);
      $ pips2 = str_replace函数(,'',$ pips1'。');
        如果($ pips2℃,)
      {
        $ pips2 = str_replace函数(' - ','',$ pips2);
        $ pips2 = LTRIM($ pips2,'0');
        $ pips2 = -1 * ABS($ pips2);
      }
      其他 {
        $ pips2 = LTRIM($ pips2,'0');
      }
      $ pips3 = $ pips2 / $ minipipskiller;
    } // PIP计数
  ELSEIF($行['类型'] ==卖出)//对于PIP计数
    {

      $ minipipskiller = '10';
      $ bidopen = $行['bidprice'];
      $点数= $ bidopen- $ bidpricepl;
      $ closedb = $ bidopen;
      $ pips1 = ROUND($个点,6);
      $ pips2 = str_replace函数(,'',$ pips1'。');
      如果($ pips2℃,)
      {
        $ pips2 = str_replace函数(' - ','',$ pips2);
        $ pips2 = LTRIM($ pips2,'0');
        $ pips2 = -1 * ABS($ pips2);
      }
      其他 {
        $ pips2 = LTRIM($ pips2,'0');
      }
      $ pips3 = $ pips2 / $ minipipskiller;
    }



      $ ticksize =0.0001; //盈亏
      $ LOT1 =100000;
      $ sizecalc = $行['大小'] * $ LOT1;

        如果($行['类型'] ==买入)
      {
        $ profitandloss = $ sizecalc * $ ticksize * $ pips3; //每笔交易
      }
      如果($行['类型'] ==卖出)
      {
        $ profitandloss = $ sizecalc * $ ticksize * $ pips3; //每笔交易
      }

      $零='0';

      如果($ profitandloss< $零){
            $ profitText =< D​​IV的风格= \颜色:红色; \> $ profitandloss< / DIV>中;
        } ELSEIF($ profitandloss> $零){
            $ profitText =< D​​IV的风格= \颜色:绿色; \> $ profitandloss< / DIV>中;
        }


$总和+ = $ profitandloss;


    回声< TR>< TD>中。 $行['trade_id']。
        &所述; / TD>&其中; TD>中。 $行['选择'。
        &所述; / TD>&其中; TD>中。 $行['日期'。
        &所述; / TD>&其中; TD>中。 $行[型。
        &所述; / TD>&其中; TD>中。 $行['大小']。
        &所述; / TD>&其中; TD>中。 $行['bidprice']。
        &所述; / TD>&其中; TD>中。 $行['offerprice']。
        &所述; / TD>&其中; TD>中。 $行['止损'。
        &所述; / TD>&其中; TD>中。 $行['赢利'。
        &所述; / TD>&其中; TD>中。 $ profitText。
        < / TD>< TD>< A HREF ='delete.php的id =?。
        $行['trade_id']'>,X< / A>
       &所述; / TD>&所述; / TR>中;
$ profitandloss = 0;

如果($总和< $零){
    $ sumText =< D​​IV的风格= \颜色:红色; \> $总和< / DIV>中;
} ELSEIF($总和> $零){
    $ sumText =< D​​IV的风格= \颜色:绿色; \> $总和< / DIV>中;
}
}

回声< /表>< BR>中;



?>
 

解决方案

我想的 的setInterval jQuery.load 是你正在寻找

 无功表= $(#TABLEID);

//刷新每5秒
VAR复习=的setInterval(函数(){
  table.load(/路径/到/ js.php);
},5000);
 

或缩短它与

  VAR复习=的setInterval(table.load.bind(表中,/路径/到/数据),5000);
 

如果你曾经想停止更新数据,(例如)说,用户离开该页面打开很长一段时间

  //停止30分钟后令人耳目一新
的setTimeout(函数(){
  clearTimeout(进修);
},180万);
 


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

 无功表= $(#TABLEID);

变种刷新=功能(){
  table.load(/路径/到/ js.php,函数(){
    的setTimeout(刷新,5000);
  });
};

刷新();
 

I am attempting to refresh a table i have as the variables in there are constantly updated and i want to re-update those variable every few seconds. I have already done up code on my part by giving the table an id and creating a div for it. The code will explain what i actually. Thanks in Advance ! EDITED: Added Var Table and Var Refresher. however the code still wont reload my table !Any ideas??

Test.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>";



?>

解决方案

I think setInterval with jQuery.load is the one you're looking for

var table = $("#tableID");

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

Or shorten it up with

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

If you'd ever like to stop refreshing the data, (e.g.,) say the user leaves the page open for a long time

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


If your data load takes a while, you might want to only refresh X seconds after the data is loaded. You could do that like this using setTimeout

var table = $("#tableID");

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

refresh();

这篇关于自动刷新一个HTML表格每x秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆