我最近開始在一個小項目中探索使用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));
}
}
?>
感謝您提供有關數據庫鎖定的提示。幾次谷歌搜索,我檢查了我的數據庫鎖,並且在2000〜4000之間跳躍! 將鎖定設置稍微嚴格一些後,我將它們降低到20〜90.我將在另一天監控頁面,以便在市場處於最繁忙狀態並且交易快速發生時查看它它已經感覺有點可靠了。 – BOMEz 2010-08-06 19:47:34