2010-08-06 59 views
1

我最近开始在一个小项目中探索使用AJAX,虽然它不够流畅,但我已经取得了合理的成功。Ajax页面并不一致地更新

基本设置是一个名为ProphetX的应用程序,它与Excel接口显示股票市场价格。价格随Excel更改而更新。每次价格更新时,使用VBA将电子表格中的数据保存到SQL08数据库中。有时可能每秒钟几次。

在Apache服务器上使用PHP我连接到SQL DB并将数据加载到表中,并使用JavaScript函数每秒更新一次信息。不过,我注意到有时候,如果你已经拥有了该页面,页面将会简单地挂起,或者如果你将它拉起来,则加载一个空白的屏幕,特别是当数据被快速更新时。因此,我的问题的肉:我的代码中是否有可能导致此呃逆的东西?我怀疑它正在监视网络或服务器资源,因为它们看起来很低。

另外我用WireShark监视网络流量,当我加载页面并在浏览器中显示空白时,我可以看到HTML从服务器发送到我的机器。

任何帮助/编码风格的批评是非常赞赏,源代码如下。

的index.php:

<html> 
<head> 
<script type="text/javascript" src="update.js"></script> 
</head> 
<body onLoad = update()> 
<font size = +2> 
<?php 
echo " 
<div id = 'marketData'></div> 
"; 
?> 
</font> 
</body></html> 

Update.js:

function update() 
{ 
    if (window.XMLHttpRequest) 
     {// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
     } 
    else 
     {// code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
    xmlhttp.onreadystatechange=function() 
    { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) 
     { 
      document.getElementById("marketData").innerHTML=xmlhttp.responseText; 
     } 
    } 
    //URL needs a var to be passed via get for code to function in IE, thus Math.Random(). 
//I am also confused by this requirement. 
    xmlhttp.open("GET","update.php?i="+ Math.random(),true); 
    xmlhttp.send(); 
    var t = setTimeout("update()", 3000); 
} 

Update.php:

<?php 
//connect to database 
error_reporting(0); 
    sqlsrv_configure("WarningsReturnAsErrors", 1); 
    $server = "myServer"; 
    $db = "myDB"; 
    $connectionInfo = array("Database"=>"$db, "UID"=>"user", "PWD"=>"pass"); 
    $conn = sqlsrv_connect($server, $connectionInfo); 

    if($conn) 
    { 
     echo "<font size =-1 color=green>Connection Established<br></font>"; 
    } 
    else 
    { 
     echo"Connection not established:<br>"; 
     print_r(sqlsrv_errors()); 
    } 

    //Func calls sqlsrv_query($conn, [$symbol],[$DatabaseName]) 
    $stmt = sqlsrv_query($conn,query(array('sym1','sym2','sym3'), "electronic")); 
    errorCheck($stmt); 
    printTables("Electronic Commodity Prices", $stmt); 

    $stmt = sqlsrv_query($conn,query(array('sym1','sym2','sym3'), "floor")); 
    errorCheck($stmt); 
    printTables("Floor Commodity Prices", $stmt); 

    $stmt = sqlsrv_query($conn,query(array('sym1','sym2','sym3',... ,sym19), "natgas")); 
    errorCheck($stmt); 
    printTables("Natural Gas Commodity Prices", $stmt); 

    sqlsrv_free_stmt($stmt); 
    sqlsrv_close($conn); 

    //This function prints out the tables 
    function printTables($tableName, $stmt) 
    { 
     echo 
     " 
     $tableName<hr> 
     <table cellspacing ='5' cellpadding = '5'> 
     <tr> 
      <th>Symbol</th> 
      <th>Product</th> 
      <th>Last Price</th> 
      <th>Change</th> 
      <th>High Price</th> 
      <th>Low Price</th> 
      <th>Previous Price</th> 
      <th>Trade Time</th> 
     </tr> 
     "; 
     while($row=sqlsrv_fetch_array($stmt)) 
     { 
     echo 
     " 
     <tr> 
      <td>$row[symbol]</td> 
      <td><font size =+3 color = blue>$row[description]</font></td> 
      <td>$row[last]</td> 
      <td><font size =+5> $row[change]</font></td> 
      <td>$row[highPrice]</td> 
      <td>$row[lowPrice]</td> 
      <td>$row[previousprice]</td> 
      <td>" . date("j M g:i",strtotime($row['tradetime'])) . "</td> 
     </tr> 
     "; 
     } 
     echo"</table><hr>"; 
    } 
    function query($symbols, $db) 
    { 
    $count = count($symbols); 
     $stmt = 
     " 
       select distinct id,symbol,description,last,change,highPrice,lowPrice,previousprice,tradetime from $db 
       where "; 
       for($i = 0; $i< $count; $i++) 
       { 
        $stmt .= "id in (select MAX(id)from $db where symbol ='$symbols[$i]') "; 
        if($i != $count-1) 
        { 
         $stmt.= "or "; 
        } 
       } 
       $stmt .= "order by description asc"; 
       // id in (select MAX(id)from $db where symbol ='$symbols[0]') 
       // or id in (select MAX(id)from $db where symbol ='$symbols[1]') 
       // or id in (select MAX(id)from $db where symbol ='$symbols[2]') 
        // order by description asc 
     return $stmt; 
    } 
    function errorCheck($stmt) 
    { 
     if($stmt=== false) 
     { 
      echo "Error in statement preparation/execution.\n"; 
      die(print_r(sqlsrv_errors(), true)); 
     } 
    } 
?> 

回答

1

没有本地运行你的代码,我建议你看看DB锁定问题。如果您真的每秒更新一次数据库记录,并且试图每三秒钟加载一次查询页面(您的超时设置为3000,即三秒钟),那么您的数据库记录将会被更新。如果您的查询页面由于数据库锁定问题而需要三秒以上的加载时间,那么您的浏览器可能会遇到阻塞问题,因为它正在等待来自一个请求的响应,同时触发新的ajax请求。

你可以尝试在你的php查询页面放一些超时代码。

+0

感谢您提供有关数据库锁定的提示。几次谷歌搜索,我检查了我的数据库锁,并且在2000〜4000之间跳跃! 将锁定设置稍微严格一些后,我将它们降低到20〜90.我将在另一天监控页面,以便在市场处于最繁忙状态并且交易快速发生时查看它它已经感觉有点可靠了。 – BOMEz 2010-08-06 19:47:34