Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php jQuery倒计时:客户端的serverSync仍然延迟1-2秒_Php_Ajax_Sync_Jquery Countdown - Fatal编程技术网

Php jQuery倒计时:客户端的serverSync仍然延迟1-2秒

Php jQuery倒计时:客户端的serverSync仍然延迟1-2秒,php,ajax,sync,jquery-countdown,Php,Ajax,Sync,Jquery Countdown,我正在构建拍卖网站,我正在使用jQuery倒数计时来显示拍卖的结束时间。我需要为我的所有客户端与服务器同步倒计时。到目前为止,我已经用serverSync函数实现了jQuery倒计时。它工作正常,但几秒钟过后,倒计时会延迟1或2秒。 如果可能的话,如何为所有客户端完美地同步时间? var syncDate = new Date("'.$formatEndDate.'"); jq("#defaultCountdown-'.$bidded_id.'").countdown("option", {

我正在构建拍卖网站,我正在使用jQuery倒数计时来显示拍卖的结束时间。我需要为我的所有客户端与服务器同步倒计时。到目前为止,我已经用
serverSync
函数实现了jQuery倒计时。它工作正常,但几秒钟过后,倒计时会延迟1或2秒。 如果可能的话,如何为所有客户端完美地同步时间?

var syncDate = new Date("'.$formatEndDate.'");  
jq("#defaultCountdown-'.$bidded_id.'").countdown("option", {until: syncDate, serverSync: serverTime});
更新:我还在轮询(500毫秒间隔)php文件,该文件更新了所有拍卖(实时效果),在这段代码中,我还从数据库中回显了新的“轮询”倒计时(因为当用户在拍卖的最后10秒内出价时,倒计时会上升+10秒)。所以我认为这就是问题所在,因为在客户端,并不是每个轮询都在同一时间启动,这取决于客户端何时刷新页面,所以会影响倒计时基本上每隔500毫秒我在poll.php中调用一次(从数据库更新倒计时):

var syncDate = new Date("'.$formatEndDate.'");  
jq("#defaultCountdown-'.$bidded_id.'").countdown("option", {until: syncDate, serverSync: serverTime});
到目前为止,我的代码延迟了1或2秒:

$(function () {
   var d = new Date("'.$endingDate.'");             
   $('#defaultCountdown').countdown({until: d,  onExpiry: endAuction, 
     expiryText: '<div class="bid-over">Ended</div>', format: 'HMS', 
     onTick: watchCountdown, serverSync: serverTime });
});

function serverTime() {
var time = null;
var url = "serverTime.php";
jq.ajax({
    url: url,
    async: false,
    dataType: "text",
    success: function (text) {
        time = new Date(text);
    },
    error: function (http, message, exc) {
        time = new Date();

    }
});
return time;
}
<?php
//This is how I get server time
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 
header("Expires: Fri, 1 Jan 2010 00:00:00 GMT"); // Date in the past 
header("Content-Type: text/plain; charset=utf-8"); // MIME type 
date_default_timezone_set('Europe/Ljubljana');
$srvDate = getdate();
$d = $srvDate['mday'];
$m = $srvDate['mon'];
$y = $srvDate['year'];
$h = $srvDate['hours'];
$i = $srvDate['minutes'];
$s = $srvDate['seconds'];
$nowDate=date("$y-$m-$d $h:$i:$s");
$nowDate=str_replace("-","/",$nowDate);
echo $nowDate;
?>
$(函数(){
var d=新日期(“.$endingDate.”);
$(#defaultCountdown')。倒计时({until:d,onExpiry:endAuction,
expiryText:“已结束”,格式为“HMS”,
onTick:watchCountdown,serverSync:serverTime});
});
函数serverTime(){
var-time=null;
var url=“serverTime.php”;
jq.ajax({
url:url,
async:false,
数据类型:“文本”,
成功:函数(文本){
时间=新日期(文本);
},
错误:函数(http、消息、exc){
时间=新日期();
}
});
返回时间;
}
serverTime.php:

$(function () {
   var d = new Date("'.$endingDate.'");             
   $('#defaultCountdown').countdown({until: d,  onExpiry: endAuction, 
     expiryText: '<div class="bid-over">Ended</div>', format: 'HMS', 
     onTick: watchCountdown, serverSync: serverTime });
});

function serverTime() {
var time = null;
var url = "serverTime.php";
jq.ajax({
    url: url,
    async: false,
    dataType: "text",
    success: function (text) {
        time = new Date(text);
    },
    error: function (http, message, exc) {
        time = new Date();

    }
});
return time;
}
<?php
//This is how I get server time
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 
header("Expires: Fri, 1 Jan 2010 00:00:00 GMT"); // Date in the past 
header("Content-Type: text/plain; charset=utf-8"); // MIME type 
date_default_timezone_set('Europe/Ljubljana');
$srvDate = getdate();
$d = $srvDate['mday'];
$m = $srvDate['mon'];
$y = $srvDate['year'];
$h = $srvDate['hours'];
$i = $srvDate['minutes'];
$s = $srvDate['seconds'];
$nowDate=date("$y-$m-$d $h:$i:$s");
$nowDate=str_replace("-","/",$nowDate);
echo $nowDate;
?>

请参阅:除了使用此功能设置脚本中的默认时区外,您还可以使用INI设置date.timezone来设置默认时区。您还可以将
getdate
代码替换为
echo日期('Y/m/d h:i:s')
。这可能会使serverTime.php更快。我将尝试使用INI。这不是
echo日期('Y/m/dh:i:s')
clients日期吗?我需要来自服务器的日期,这就是为什么我
getDate()
。或者有没有“更快”的方法来获取服务器时间而不是客户端时间?date()将返回服务器日期/时间