我有一个问题。我正在创建像BidRivals这样的拍卖网站。
我目前正在使用autcion脚本并完全正常工作,但我必须每5秒钟重新加载页面以检查是否有新的出价,这在我看来并不是一个好方法。
所以我正在优化代码,以便只有新的出价才能刷新页面。
目前我有这段代码:
poll.php
include('../configs/db_ligar.php'); // db connection stuff..
$query = "SELECT auction_id, auction_bid_counter FROM leilao WHERE auction_product_id = $id_prod";
$result = mysql_query($query);
if(mysql_num_rows($result) == 0) {
echo -1;
} else {
$resultArray = array();
$row = mysql_fetch_array($result);
$resultArray['auction_id'] = $row['auction_id'];
$resultArray['auction_bid_counter'] = $row['auction_bid_counter'];
//Fast fix ... not pretty :)
echo "[" . json_encode($resultArray) . "]";
}
的index.php
$(document).ready(function()
{
var checkTable = setInterval(function(){
$.post('poll.php', function(resultData){
//If there is data
if(!resultData == -1){
//DIFFERENT: need to get the first element from the eval array
var resultObject = eval(resultData)[0];
alert('auction_id: ' + resultObject.auction_id + ' - auction_bid_counter: ' + resultObject.leilao_bid_counter);
}
}, 1000);
});
});
此代码是否包含在内?