2010-04-09 52 views
1

無法獲得正確的答案,因爲我從Jquery變量獲得了正確的結果「很快」,但是當我與函數「serverSync」同步時,所有將設置爲0:0:0我檢查了兩個具有相同日期。 ref。網站 http://keith-wood.name/countdown.html如何倒計時使用「jquery.countdown.js」插件與jquery同步?

這裏是我的代碼

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

HTML文件

<script type="text/javascript" src="Scripts/jquery-1.3.2.js"></script> 

<script type="text/javascript" src="Scripts/jquery.countdown.js"></script> 

<script type="text/javascript"> 
    $(function() { 
     var shortly = new Date('April 9, 2010 22:38:10'); 
     var newTime = new Date('April 9, 2010 22: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'); 
      } 
     }); 

     return time; 

    } 
    function watchCountdown() { } 
    function liftOff() { } 

</script> 


+0

如果您從圖片中取出倒計時插件,您的Webmethod是否會返回一個值? – offner 2010-04-09 16:42:09

+0

重複問題:http://stackoverflow.com/questions/2608838/having-problem-with-jquery-countdown-function-serversync-servertime和http://stackoverflow.com/questions/2608389/unable-to-get-我正在調用servertime使用jquery倒計時 – dochoffiday 2010-04-09 20:28:28

回答

0

您正在設置的服務器時間等於您倒計時的時間。

由於新的「serverSync」時間和「until」時間相同,因此倒計時將全部爲0。

+0

謝謝文檔hoffiday 但問題我設置「短」變量和「serverSync」返回相同的日期。短期日期工作正常,但「serverSync」顯示0的全局 我需要運行將從「serverSync」函數來的倒計時。 – 2010-04-10 06:33:08