2011-04-29 43 views
1

我已經在我的PHP文件中的以下代碼:同步與服務器時間 - jQuery的倒計時插件和PHP

<div id="clockTimestamp" class="hidden"><?=$_SERVER['REQUEST_TIME'];?></div> 
<div id="clock"></div> 

我使用從Keith Wood jQuery的倒計時插件和我打電話這樣說:

$('#countdown').countdown({until: until, serverSync: getServerTime}); 

,按照該js文件代碼:

function getServerTime() { 
    var time = $('#clockTimestamp').html()*1000; 
    time = new Date(time); 
    return time; 
} 

現在,我的問題是:THI與$ _ SERVER [「REQUEST_TIME」] OK的做法還是應該做這樣的:

function getServerTime() { 
    var time = null; 
    $.ajax(
    { 
     url: '../../_ajax/getServerTime.php', 
     async: false, 
     dataType: 'text', 
     success: function(text) { 
      time = new Date(text); 
     }, 
     error: function(http, message, exc) { 
      time = new Date(); 
     } 
    }); 
    return time; 
} 

回答

1

我敢肯定你已經解決了現在這個樣子,而且也爲以後的遊客....

最簡單的方法是使用jQuery server time plugin

+0

不太確定它應該如何工作。它會掛鉤到標準的日期和時間函數中,並將它們返回到正確的服務器時間,或者如果我使用當前時間和日期創建Date對象,是否必須通過插件的函數運行它?沒有真正的API文檔。 – inquam 2014-08-18 20:14:16

+0

你的答案僅僅是一個評論,而不是一個答案..它不是很容易,你所提供的這個所謂的腳本甚至不在github上,而是在某些服務器上的一些壓縮文件中,比如在90年代,並有很多代碼...沒有文檔..我不會使用這樣的事情 – vsync 2014-09-14 12:43:23