2014-11-23 42 views
0

我需要向我的腳本添加一個獲取當前服務器時間的函數。向腳本添加多個AJAX調用

我使用下面的PHP文件獲取服務器時間,以毫秒爲單位。

<?php 
date_default_timezone_set('Europe/London'); 
$serverTime = round(microtime(true) * 1000); 
echo json_encode($serverTime); 
?> 

然後我想補充一個Ajax請求「得到」 serverTime.PHP,並把它變成一個變量,以便正確地事情結束之前,我可以計算時間。

我目前使用此

var now = new Date().getTime(); 

現在我想刪除該行並添加我的Ajax請求得到了客戶的時間。

我曾嘗試加入如下代碼到腳本,但我不能讓它執行

function now() 
{ 
    this.ajax.open('GET', 'serverTime.php', 
        true); 
    this.ajax.send(null);  

    if (this.ajax.readyState != 4) return; 
    if (this.ajax.status == 200) 
    { 
     // get response 
     var now = eval ('('+this.ajax.responseText+')'); 
    } 
} 

最終的結果是變量「NOW」包含serverTime.PHP

的輸出

這裏是我的腳本,我試圖以各種方式添加anothert ajax get請求,但我不能讓它正常工作。

$.ajaxSetup({ 
    type: 'GET', 
    headers: { "cache-control" : "no-cache" } 
}); 

var PlayList = function (onUpdate, onError) 
{ 
    // store user callbacks 
    this.onUpdate = onUpdate; 
    this.onError = onError; 

    // setup internal event handlers 
    this.onSongEnd = onSongEnd.bind (this); 

    // allocate an Ajax handler 
    try 
    { 
     this.ajax = window.XMLHttpRequest 
      ? new XMLHttpRequest() 
      : new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    catch (e) 
    { 
     // fatal error: could not get an Ajax handler 
     this.onError ("could not allocated Ajax handler"); 
    } 
    this.ajax.onreadystatechange = onAjaxUpdate.bind(this); 

    // launch initial request 
    this.onSongEnd(); 

    // ------------------------------------------ 
    // interface 
    // ------------------------------------------ 

    // try another refresh in the specified amount of seconds 
    this.retry = function (delay) 
    { 
     setTimeout (this.onSongEnd, delay*5000); 
    } 

    // ------------------------------------------ 
    // ancillary functions 
    // ------------------------------------------ 

    // called when it's time to refresh the playlist 
    function onSongEnd() 
    { 
     // ask for a playlist update 
     this.ajax.open('GET', 'playlist.php', // <-- reference your PHP script here 
         true); 
     this.ajax.send(null);  
    } 

    // called to handle Ajax request progress 
    function onAjaxUpdate() 
    {  
     if (this.ajax.readyState != 4) return; 
     if (this.ajax.status == 200) 
     { 
      // get response 
      var list = eval ('('+this.ajax.responseText+')'); 

      // compute milliseconds remaining till the end of the current song 
      var start = new Date(list[0].date_played.replace(' ', 'T')).getTime(); 
      var now = new Date (         ).getTime(); 
      var d = start - now + 6500 
        + parseInt(list[0].duration); 
      if (d < 0) 

      { 
       // no new song started, retry in 3 seconds 
       d = 3000; 
      } 
      else 
      { 
       // notify caller 
       this.onUpdate (list); 
      } 

      // schedule next refresh 
      setTimeout (this.onSongEnd, d); 

     } 
     else 
     { 
      // Ajax request failed. Most likely a fatal error 
      this.onError ("Ajax request failed"); 
     }  
    } 
} 

var list = new PlayList (playlistupdate, playlisterror); 

function playlistupdate (list) 
{ 
for (var i = 0 ; i != list.length ; i++) 
    { 
     var song = list[i]; 

    } 
{ 

    document.getElementById("list0artist").innerHTML=list[0].artist; 
    document.getElementById("list0title").innerHTML=list[0].title; 
    document.getElementById("list0label").innerHTML=list[0].label; 
    document.getElementById("list0albumyear").innerHTML=list[0].albumyear; 
    document.getElementById("list0picture").innerHTML='<img src="/testsite/covers/' + list[0].picture + '" width="170" height="170"/>'; 

    document.getElementById("list1artist").innerHTML=list[1].artist; 
    document.getElementById("list1title").innerHTML=list[1].title; 
    document.getElementById("list1label").innerHTML=list[1].label; 
    document.getElementById("list1albumyear").innerHTML=list[1].albumyear; 
    document.getElementById("list1picture").innerHTML='<img src="/testsite/covers/' + list[1].picture + '" width="84" height="84"/>'; 

    document.getElementById("list2artist").innerHTML=list[2].artist; 
    document.getElementById("list2title").innerHTML=list[2].title; 
    document.getElementById("list2label").innerHTML=list[2].label; 
    document.getElementById("list2albumyear").innerHTML=list[2].albumyear; 
    document.getElementById("list2picture").innerHTML='<img src="/testsite/covers/' + list[2].picture + '" width="84" height="84"/>'; 

    document.getElementById("list3artist").innerHTML=list[3].artist; 
    document.getElementById("list3title").innerHTML=list[3].title; 
    document.getElementById("list3label").innerHTML=list[3].label; 
    document.getElementById("list3albumyear").innerHTML=list[3].albumyear; 
    document.getElementById("list3picture").innerHTML='<img src="/testsite/covers/' + list[3].picture + '" width="84" height="84"/>'; 

    document.getElementById("list4artist").innerHTML=list[4].artist; 
    document.getElementById("list4title").innerHTML=list[4].title; 
    document.getElementById("list4label").innerHTML=list[4].label; 
    document.getElementById("list4albumyear").innerHTML=list[4].albumyear; 
    document.getElementById("list4picture").innerHTML='<img src="/testsite/covers/' + list[4].picture + '" width="84" height="84"/>'; 

$('.nowPlayNoAnimate').each(function() { 
    $(this).toggleClass('nowPlayAnimate', $(this).parent().width() < $(this).width()); 
}); 

} 
} 

function playlisterror (msg) 
{ 
    // display error message 
    console.log ("Ajax error: "+msg); 

    //retry 
    list.retry (10); // retry in 10 seconds 
} 
+0

你發佈的這段代碼全部正常工作,並且你正試圖添加另一個電話?你能發表你試過的嗎? – 2014-11-23 18:51:00

+0

@Joe T我已經添加NOW功能,我一直在玩 – Justin 2014-11-23 19:06:02

回答

1

爲什麼不使用jquery方法?

function getServerTime() { 
    var now = null; 
    $.ajax({ 
    url: "serverTime.php", 
    dataType: 'JSON', 
    async: false, 
    success: function (data) { 
     now = data; 
    } 
    }); 
    return now; 
} 

您可以以瀏覽器便攜的方式開始儘可能多的請求。

PS: 您可能還需要在更短的 $("#list4artist").html(list[4].artist);

編輯取代 document.getElementById("list4artist").innerHTML=list[4].artist;

:添加async參數,使執行等待Ajax調用來模擬一個非異步函數調用。

+0

可悲的是由於我的瘦知識,我不知道如何將其添加到腳本,使NOW變量讀取您的結果。 – Justin 2014-11-23 19:07:17

1

假設你的服務返回日期對象,你需要添加成功函數內以下行(克萊門特·普雷沃建議):服務器後觸發

var now = data; 

成功函數是一個異步回調函數返回一個值。 你應該閱讀關於JQuery的Ajax,它會讓你的生活更輕鬆。