如何使用Ajax从服务器的时间? [英] How to get time from server using ajax?
问题描述
我想创建像一分钱拍卖网站如何显示倒计时时间呢? 我试过用ajax,但有时它吞下一两秒钟,它表明秒像10,9,7,6,3 ......我的意思是它并没有显示正确的倒计时..请帮我解决这个问题
这是我的code
< PHP
@session_start();
包括包括/的common.php;
include_once包括/班/ class.Auction.php;
$ objAuction =新的拍卖();
$结果= $ objAuction - >的getStatus();
回声$结果;
?>
// AJAX code
函数getStatusOne(PID)
{
VAR strURL =get_status_one.php PID =?+ PID;
VAR REQ = getXMLHTTP();
如果(REQ)
{
req.onreadystatechange =功能()
{
如果(req.readyState == 4)
{
如果(req.status == 200)
{
//alert(req.responseText);
VAR的结果= req.responseText.substr(1).split(|);
为(变种X = 0 X - 其中; result.length; X ++)
{
变种resultN =结果[X] .split(,);
变种PRID = resultN [0];
变种临时= resultN [1];
VAR秒= parseInt函数(临时);
变种价格= resultN [2];
//警报(PRID +''+温度+''+价格);
变种纪念品= resultN [3];
变种的img = resultN [4];
变种自动出价= resultN [5];
如果(IMG ==''){
IMG ='图片/资料/ no_image.jpg
}
如果(!价格)
{
价格='0.00';
}
如果(!MEM)
{
纪念品='没有竞买人呢。;
}
如果(的document.getElementById(bid_price+ PRID))
{
的document.getElementById(bid_price+ PRID).innerHTML ='$'+的价格;
的document.getElementById(bidder_name+ PRID).innerHTML =纪念品;
的document.getElementById(userimg)SRC = IMG。
。的document.getElementById(bid_rate)的innerHTML =自动出价;
如果(SEC< = -1)
{
出售(PRID);
如果(的document.getElementById('端'+ PID))
{
的document.getElementById('端'+ PID).style.display =块;
}
如果(的document.getElementById('div_bid_image'))
{
。的document.getElementById('div_bid_image')的style.display =none的;
}
如果(的document.getElementById('clsBidB'+ PID))
{
的document.getElementById('clsBidB'+ PID).style.display =无;
}
}
其他 {
如果(的document.getElementById('div_bid_image')。的style.display = =无)
{
。的document.getElementById('div_bid_image')的style.display =块;
}
如果(SEC> = 0)
{
SetCountdownText(秒,div_timer+ PRID,PRID);
}
}
}
}
}
其他
{
//警报(同时使用XMLHTTP有一个问题:\ N+ req.statusText);
}
}
}
req.open(POST,strURL,真正的);
req.send(空);
}
}
// PHP code,计算时间
的getStatus函数()
{
$ selProd =选择a.pdt_id,UNIX_TIMESTAMP(a.end_date) - UNIX_TIMESTAMP('年月日^ h日期(。':I:S')。)为秒,b.bid_price,c.uname从tbl_products左加入tbl_auction B关于a.pdt_id = b.product_id左连接上b.member_id = c.member_id tbl_members C,其中(选择UNIX_TIMESTAMP(a.end_date) - UNIX_TIMESTAMP('的日期(YMD H:我:S')。 '))> = 0;
如果($这 - >的executeQuery($ selProd,norows)大于0)
{
$ auctionArr = $这个 - >的executeQuery($ selProd,选择);
$ auctionName = $这个 - > array2str($ auctionArr);
}
返回$ auctionName;
}
功能array2str($数组,$级= 1)
{
$海峡=阵列();
的foreach($数组作为$关键=> $值){
如果(is_int($键))
{
$ nkey = $键;
$ n值= is_array($值)'|'$这个 - > array2str($值):$值。
$海峡[] = $ n值;
}
}
返回破灭(',',$ STR);
}
假设你有类似ID为倒计时的DIV(以显示倒计时):
示例的JavaScript(假设使用jQuery的 - 推荐):
(函数(jQuery的){
updateCountdown(倒计时); //调用的页面加载
VAR倒计时=的setInterval('updateCountdown(倒计时),1000); //更新倒计时每一秒
})(jQuery的);
功能updateCountdown(elementId){
jQuery.ajax({
网址:/ajax/countdown.php?auctionId=123
键入:GET,
数据类型:JSON,
成功:函数(响应){
//将值转换成目标元素
jQuery的(#+ elementId)。html的(反应[timeRemaining]);
//停止倒计时完成时
如果(响应[countdownComplete] ==真)
clearInterval(倒计时);
}
});
}
示例PHP脚本(假设在/ajax/countdown.php通过上面的JavaScript):
&LT ;? PHP
/ *在这里插入自己的逻辑* /
$响应[timeRemaining] =10秒;
$响应[countdownComplete] = FALSE; //设置为true时,倒计时完成
回声json_en code(响应);
?>
我建议你做所有的计算服务器端(在PHP),因为它具有真正优秀的时间/日期处理(有很多内置的方法),并需要更少的code的整体实现。
I am trying to create a website like penny auction how to display the count down time? i tried that using ajax, but sometimes it swallow one or two seconds, it shows seconds like 10,9,7,6,3... i mean it doesn't show the proper count down time.. please help me to solve this problem
here is my code
<?php
@session_start();
include "includes/common.php";
include_once "includes/classes/class.Auction.php";
$objAuction = new Auction();
$result=$objAuction -> getStatus();
echo $result;
?>
//ajax code
function getStatusOne(pId)
{
var strURL="get_status_one.php?pId="+pId;
var req = getXMLHTTP();
if (req)
{
req.onreadystatechange = function()
{
if (req.readyState == 4)
{
if (req.status == 200)
{
//alert(req.responseText);
var result= req.responseText.substr(1).split("|");
for (var x = 0; x < result.length; x++)
{
var resultN=result[x].split(",");
var prId=resultN[0];
var temp=resultN[1];
var sec=parseInt(temp);
var price=resultN[2];
//alert(prId+' '+temp+' '+price);
var mem=resultN[3];
var img=resultN[4];
var autobid=resultN[5];
if(img=='') {
img='images/profile/no_image.jpg'
}
if(!price)
{
price='0.00';
}
if(!mem)
{
mem='No Bidders Yet';
}
if(document.getElementById("bid_price"+prId))
{
document.getElementById("bid_price"+prId).innerHTML='$'+price;
document.getElementById("bidder_name"+prId).innerHTML=mem;
document.getElementById("userimg").src=img;
document.getElementById("bid_rate").innerHtml=autobid;
if(sec<= -1)
{
sold(prId);
if(document.getElementById('end'+pId))
{
document.getElementById('end'+pId).style.display="block";
}
if(document.getElementById('div_bid_image'))
{
document.getElementById('div_bid_image').style.display="none";
}
if(document.getElementById('clsBidB'+pId))
{
document.getElementById('clsBidB'+pId).style.display="none";
}
}
else {
if(document.getElementById('div_bid_image').style.display == "none")
{
document.getElementById('div_bid_image').style.display="block";
}
if(sec >=0)
{
SetCountdownText(sec,"div_timer"+prId,prId);
}
}
}
}
}
else
{
//alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("POST", strURL, true);
req.send(null);
}
}
//php code to calculate time
function getStatus()
{
$selProd="select a.pdt_id, unix_timestamp(a.end_date) - unix_timestamp('".date('Y-m-d H:i:s')."') as seconds, b.bid_price,c.uname from tbl_products a left join tbl_auction b on a.pdt_id=b.product_id left join tbl_members c on b.member_id=c.member_id where(select unix_timestamp(a.end_date) - unix_timestamp('".date('Y-m-d H:i:s')."'))>= 0 ";
if($this->ExecuteQuery($selProd,"norows") > 0)
{
$auctionArr=$this->ExecuteQuery($selProd,"select");
$auctionName=$this->array2str($auctionArr);
}
return $auctionName;
}
function array2str($array,$level=1)
{
$str = array();
foreach($array as $key=>$value) {
if(is_int($key))
{
$nkey = $key;
$nvalue = is_array($value)?'|'.$this->array2str( $value ) : $value;
$str[] = $nvalue;
}
}
return implode(',',$str);
}
Assuming you have something like a DIV with the ID "countdown" (to display the countdown in):
Example JavaScript (assumes use of jQuery - recommended):
(function(jQuery) {
updateCountdown("countdown"); // Call on page load
var countdown = setInterval('updateCountdown("countdown")', 1000); // Update countdown every second
})(jQuery);
function updateCountdown(elementId) {
jQuery.ajax({
url: "/ajax/countdown.php?auctionId=123",
type: "GET",
dataType: "json",
success: function(response) {
// Insert value into target element
jQuery("#"+elementId).html(response["timeRemaining"]);
// Stop countdown when complete
if (response["countdownComplete"] == true)
clearInterval(countdown);
}
});
}
Example PHP script (assumed to be at /ajax/countdown.php by the above JavaScript):
<? php
/* Insert your own logic here */
$response["timeRemaining"] = "5 seconds";
$response["countdownComplete"] = false; // Set to true when countdown complete
echo json_encode(response);
?>
I'd recommend doing all the calculation server side (in PHP) as it has really excellent time/date handling (with lots of built in methods) and requires less code to implement overall.
这篇关于如何使用Ajax从服务器的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!