2017-02-03 105 views
-1

我試圖一遍又一遍讀取文本文件,然後根據文本更改頁面。我目前正在閱讀這些文件的方式需要很長時間,而且我在問是否有更快的方法來完成它。jquery快速讀取文件

<script> 
scoreIsShown = false 
team1 = "" 
team2 = "" 
score1 = 0 
score2 = 0 
function UrlExists(url) 
{ 
    $.get("showscore.txt", function(data) { 
     $(".result").html(data); 
     showscore = (data.toLowerCase() == "true") 

    }); 
    $.get("team1.txt", function(data) { 
     `enter code here`$(".result").html(data); 
     //(data); 
     team1 = data 

    }); 
    $.get("team2.txt", function(data) { 
     $(".result").html(data); 
     //(data); 
     team2 = data 

    }); 
    $.get("score1.txt", function(data) { 
     $(".result").html(data); 
     //(data); 
     s = (data.toLowerCase() == "true") 
     score2 = parseInt(data) 

    }); 
    $.get("score2.txt", function(data) { 
     $(".result").html(data); 
     //(data); 
     score2 = parseInt(data) 

    }); 

} 
function showScore1() 
{ 
    x = document.createElement("table") 
    tr = document.createElement("tr") 
    td = document.createElement("td") 
    team1p = document.createTextNode(team1) 
    td.appendChild(team1p) 
    tr.appendChild(td) 
    x.appendChild(tr) 
    document.body.appendChild(x) 
} 
function onload() 
{ 
    while (true){ 
     UrlExists("binary1.txt") 
     setTimeout(function() 
     { 
      if (showscore) 
      { 
       if (!scoreIsShown) 
       { 
        showScore1() 

       } 
      }else{ 
       if (scoreIsShown) 
       { 
        //hideScore() 
       } 
      } 
     }, 10) 
    } 
} 
</script> 

所有文件都是一行長,每個文件少於10個字符。

+0

不是真的回答你的問題,但有沒有一個原因,你不能將文件轉換爲json並使用ajax請求來獲取json數據? – Michael

+0

也沒有真正回答你的問題,但沒有錯誤在控制檯做它作爲一個奇怪的數據類型 - 可能會添加$ .get('showscore.txt',函數(數據){...},'文本'); – dmoo

+0

只是一個註釋:你的score1存儲它的結果得分2 :) ..就像@邁克爾說,存儲在一個簡單的JSON數據,並閱讀,會更好。但即便如此,我看不出爲什麼閱讀5個文本文件需要很長時間,您通過300波特調制解調器運行您的網站。 :) – Keith

回答

1

老兄!你正在創建一個無限循環,這就是爲什麼它花費太長時間

while (true){ 
    UrlExists("binary1.txt") //<--Everything here is async so it returns immediately 
    setTimeout(function() 
    { 
     if (showscore) 
     { 
      if (!scoreIsShown) 
      { 
       showScore1() 

      } 
     }else{ 
      if (scoreIsShown) 
      { 
       //hideScore() 
      } 
     } 
    }, 10) //<--This is also async, so it returns immediately 
    //You've done almost nothing, lets do it again! 
} 

您正在請求文件並設置超時數十次或每秒一次的時間。

做這樣的事情,而不是:

function UrlExists(url) { 
    $.when(//start a promise to keep track of every request 

     $.get("showscore.txt", function(data) { 
      $(".result").html(data); 
      showscore = (data.toLowerCase() == "true") 
     }), 
     $.get("team1.txt", function(data) { 
      $(".result").html(data); 
      //(data); 
      team1 = data 
     }), 
     $.get("team2.txt", function(data) { 
      $(".result").html(data); 
      //(data); 
      team2 = data 
     }), 
     $.get("score1.txt", function(data) { 
      $(".result").html(data); 
      //(data); 
      s = (data.toLowerCase() == "true") 
      score2 = parseInt(data) 
     }), 
     $.get("score2.txt", function(data) { 
      $(".result").html(data); 
      //(data); 
      score2 = parseInt(data) 
     }) 

    ).done(function() { 
     //when all done, continue 
     //do the thing you was doing in the while 
     if (showscore) 
     { 
      if (!scoreIsShown) 
      { 
       showScore1() 
      } 
     }else{ 
      if (scoreIsShown) 
      { 
       //hideScore() 
      } 
     } 
     //and set a timer to call the funcion again later 
     setTimeout(function() { 
      UrlExists(url); 
     },60000); 
    }); 

} 

function onload() { 
    UrlExists("binary1.txt"); 
} 

如果它仍然緩慢,這是你的服務器。我建議創建一個包含所有數據(可能是JSON或XML)的單個文件,並且每次只創建一個請求。我強烈建議你這樣做。

+0

它需要持續不斷地進行檢查,因爲它將用於實時將實時影響應用於實時流。即時搞清楚如何做到這一點,然後把它放在實際的網站 –

+0

只需在'timeOut'中使用較小的時間間隔。但是你必須擺脫循環,等待請求完成才能創建新的請求,否則你會使客戶端的瀏覽器速度太慢甚至停止,並且服務器會抓住它,甚至可能會禁止客戶端的IP。而且你不知道哪個請求先完成。您可以從最後一個請求獲得更新,然後從第一個請求獲得更新,然後第三個完成,等等。在單個請求中提取所有內容也會有所幫助。也許你可以用PHP或其他服務器端腳本將所有內容合併到一個文件中。 – Gabriel