2010-04-09 50 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