我正在使用 jquery 倒计时 serverSync 功能,如上所述 http://keith-wood.name/countdownRef.html#serverSync ,但我无法让倒计时同步到服务器时间。我的代码如文档中所写:
$(selector).countdown({
until: liftoffTime,
serverSync: serverTime
});
function serverTime() {
var time = null;
$.ajax({
url: 'http://myserver.com/serverTime.php',
async: false,
dataType: 'text',
success: function (text) {
time = new Date(text);
},
error: function (http, message, exc) {
time = new Date();
}
});
alert("Debug server time: " + time);
return time;
}
serverTime.php 中的我的 php 代码
<?php
$now = new DateTime();
echo $now->format("M j, Y H:i:s O")."\n";
?>
当我在 javascript 中发出警报时,输出的时间是:
Sun Jun 24 2012 12:02:23 GMT+0100 (BST)
但是当我访问 http://myserver.com/serverTime.php 时时间以这种格式显示:
Jun 24, 2012 12:03:35 +0100
非常不同。任何人都可以阐明为什么它不同步以及返回的不同日期格式可能是问题吗?
最佳答案
您的 php
和 serverTime()
代码工作正常。
不同的时间格式不是问题,也不是内部 Date
表示形式。
您为 selector
和 liftoffTime
变量分配什么值?
这是我的工作测试:
<html>
<head>
<title>ajax count down test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://keith-wood.name/js/jquery.countdown.js"></script> <!--maybe you shouldn't hotlink this file ;-) -->
</head>
<body>
<div id="defaultCountdown"></div>
<script>
function serverTime() {
var time = null;
$.ajax({
url: 'serverTime.php',
async: false,
dataType: 'text',
success: function (text) {
time = new Date(text);
},
error: function (http, message, exc) {
time = new Date();
}
});
return time;
}
$(function () {
$("#defaultCountdown").countdown({
until: new Date("Jun 24, 2012 16:00:00 +0000"),
serverSync: serverTime
});
});
</script>
</body>
</html>
如果您有任何疑问,请发表评论。
关于php - jquery 倒计时服务器同步不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11176934/