2017-02-03 1 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으로 변환 할 수없고 json 데이터를 가져 오기 위해 ajax 요청을 사용할 수없는 이유가 있습니까? – Michael

+0

또한 실제로 귀하의 질문에 대답하지 않지만 거기에 이상한 데이터 형식 으로이 일을 콘솔에 오류가 없습니다 - 아마도 $ .get ('showscore.txt', function (data) {...}, 'text'); – dmoo

+0

참고 사항 : score1은 점수 2의 결과를 저장합니다. :) @Michael처럼 간단한 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

관련 문제