【问题标题】:Having problem with jQuery Countdown? Function serverSync: serverTimejQuery Countdown 有问题吗?函数 serverSync: serverTime
【发布时间】:2012-12-20 00:39:59
【问题描述】:

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");
}

【问题讨论】:

标签: c# asp.net jquery


【解决方案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>

【讨论】:

    【解决方案2】:

    你不应该在这里调用 serverTime() -

    $('#defaultCountdown').countdown({
        until: shortly, onExpiry: liftOff, onTick: watchCountdown, serverSync: serverTime
    });
    

    像这样:

    $('#defaultCountdown').countdown({
        until: shortly, onExpiry: liftOff, onTick: watchCountdown, serverSync: serverTime()
    });
    

    【讨论】:

    • 我打电话给先生,但它没有显示输出
    • 尝试查看该函数是否有任何内容。 var a = serverTime();警报(a);
    • 是的,它会显示时间,但倒计时重置为 0:0:0
    【解决方案3】:
    async: true,
    

    这将解决问题,但不会同步。也许其他人可以弄清楚为什么async: false,会打破这个?我不知道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多