2012-06-22 10 views
1

일부 로컬 json 파일로 작업하고 있지만 로딩 속도에 문제가 있습니다. 내 서버는 Python으로 만든 작은 웹 서버이며 일반적으로 자바 스크립트 코드를 시험해 보는 데 사용합니다. 내 스크립트는 Firefox에서만 작동하며 60000 ms의 지연 시간 (또는 방화 거리 사용)을 사용해야합니다. 스크립트는 다음과 같습니다json 로딩 속도 향상

function geisson() 
{ 
var iabile = new XMLHttpRequest(); 

iabile.open("GET", "longanocard.json", true); 
iabile.send(null); 


PerdiTempo(60000); 

var objectjson = {}; 
var arrayCards= []; //creazione dell'array che conterrà le cards 


objectson = JSON.parse(iabile.responseText); 

arrayCards = objectson.cards; 
//alert(arrayCards[0].__guid__.toSource()); 


var qwerty = arrayCards[0].__guid__; 

var mela = "http://www.airpim.com/png/public/card/" + qwerty + "?width=292";  


document.location = mela; 
//windows.location.href= mela; 
} 

PerdiTempo입니다 내가 지연에 사용하는 기능 :

내가 파일 longanocard.json의 로딩 속도를 높일 수있는 방법
function PerdiTempo(ms) 
{ 
ms += new Date().getTime(); 
while (new Date() < ms){} 
} 

? 지연이 다른 브라우저에서 작동하지 않는 이유는 무엇입니까?

+2

을 위해 대신 onreadystatechange 이벤트를 사용, (당신이 많은 초는 JSON 구문 분석을 지연 정확히 알고 어떻게?) 그런 식의 비동기 응답을 기다리는 피해야한다 이쪽은 끔찍합니다. 'onreadystatechange' 이벤트에서 응답 상태를 체크하는 콜백으로 작업해야합니다. – fcalderan

+0

JS 코드가 끝날 때까지 브라우저가 다시 그리기를하지 않기 때문에 지연을 코딩해서는 안됩니다 갇혀있다. – nnnnnn

답변

2

당신은 정말의 응답을 기다리는 요청

function geisson() { 

    var iabile = new XMLHttpRequest(); 

    iabile.onreadystatechange = function(){ 
     if(iabile.readyState === 4 && iabile.status === 200) { 
      var objectjson = {}; 
      var arrayCards= []; //creazione dell'array che conterrà le cards 
      objectson = JSON.parse(iabile.responseText); 

      ... 
      /* codice restante qui */ 

     } 
    } 
    iabile.open("GET", "longanocard.json", true); 
    iabile.send(null); 

}