2010-04-09 53 views
0

當我使用jquery.countdown.js + Asp.net調用serverTime時無法獲得正確的時間?

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


[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

Default.aspx/GetTime代碼 public static String GetTime() DateTime dt = new DateTime(); dt = Convert.ToDateTime(「2010年4月9日22:38:10」); dt = Convert.ToDateTime(「April 9,2010 22:38:10」); } – 2010-04-09 14:52:15

+0

重複問題:http://stackoverflow.com/questions/2609028/here-is-my-code-in-jquery-and-asp-net-unable-to-get-a-correct-ans- as-i-am-gettin and http://stackoverflow.com/questions/2608838/having-problem-with-jquery-countdown-function-serversync-servertime – dochoffiday 2010-04-09 20:29:21

回答

0

回調之前返回火災;你必須使用回調來設置一些值。在成功或失敗的回調中,您需要將日期傳遞給HTML元素或分配全局變量。

HTH。

+0

hello Brian 我正在使用第一次Jquery.countdown.js 請你給我一個例子 – 2010-04-09 14:55:42

+0

您好布賴恩 $( '#defaultCountdown')倒計時({ 到:不久,onExpiry:升空,onTick:watchCountdown,serverSync:serverTime })。 serverTime =這個回調函數會初始化變量的值 – 2010-04-09 14:57:36

相關問題