serverSync:serverTime函数从服务器返回值,但我检查了服务器和客户端时间都相同。当我调用服务器与服务器同步时,它不会显示倒计时。帮帮我?
$(function() {
var shortly = new Date();
var newTime = new Date('April 9, 2010 20:38:10');
//for loop divid
///
$('#defaultCountdown').countdown({
until: shortly, onExpiry: liftOff, onTick: watchCountdown, serverSync: serverTime
});
$('#div1').countdown({ until: newTime });
});
function serverTime() {
var time = null;
$.ajax({
type: "POST",
//Page Name (in which the method should be called) and method name
url: "Default.aspx/GetTime",
// If you want to pass parameter or data to server side function you can try line
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{}",
async: false,
//else If you don't want to pass any value to server side function leave the data to blank line below
//data: "{}",
success: function(msg) {
//Got the response from server and render to the client
time = new Date(msg.d);
alert(time);
},
error: function(msg) {
time = new Date();
alert('1');
}
});
shortly = time;
return time;
}
[WebMethod]
public static String GetTime()
{
DateTime dt = new DateTime();
dt = Convert.ToDateTime("April 9, 2010 22:38:10");
return dt.ToString("dddd, dd MMMM yyyy HH:mm:ss");
}
答案 0 :(得分:1)
我意识到这已经快一年了,但是如果有人遇到类似的问题,我想到了一个可以避免AJAX复杂性的解决方案。由于我们在服务器上使用日期,因此我们不必担心客户端上的时间,因此我们无需调整时区或客户端时钟关闭。它应该与网络延迟一样准确。
<script type="text/javascript">
jQuery(document).ready(
function() {
var eventTime = new Date('March 20, 2011 08:00:00');
jQuery('#div_countdown').countdown({ until: eventTime,
serverSync: function() { return new Date('<%=DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss")%>'); }
});
}
);
</script>
答案 1 :(得分:0)
你不应该在这里调用serverTime() -
$('#defaultCountdown').countdown({
until: shortly, onExpiry: liftOff, onTick: watchCountdown, serverSync: serverTime
});
像这样:
$('#defaultCountdown').countdown({
until: shortly, onExpiry: liftOff, onTick: watchCountdown, serverSync: serverTime()
});
答案 2 :(得分:0)
async: true,
这将解决问题,但不会同步。也许其他人可以弄明白为什么async: false
会打破这个?我不知道。