Having problem with jQuery Countdown? Function ser

2020-05-09 04:31发布

问题:

serverSync: serverTime Function return value from server but I have checked both server and client time both are same.When i called server to sync with server it will not display countdown. help me ?

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

回答1:

I realize this is almost a year old, but in case anyone has a similar problem, I thought of a solution that will avoid the complexity of AJAX. Since we're using the date on the server, we don't have to worry about the time on the client so we don't have to adjust for time zone or the clients clock being off. It should be as accurate as the network latency.

<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:

Shouldn't you call serverTime() here -

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

Like this:

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


回答3:

async: true,

That will fix the problem but it will not be synchronous. Maybe someone else can figure out why the async: false, will break this? I have no clue.