2013-03-07 4 views
2

나는 ebay API에서 일부 데이터 (JSON)를 가져 오는 간단한 웹 응용 프로그램을 작성하고 각 항목의 가격을 보여주는 차트에 결과를 표시합니다. 이것은 잘 작동합니다.JSON 업데이트시 AJAX 호출

그러나 항목이 입찰가를 받거나 예를 들어 완료되면 실시간으로 차트를 업데이트하고 싶습니다. 이 모든 데이터는 eBay에서 반환 된 JSON에 들어 있습니다.

내 문제는 그래프를 업데이트하고 JSON이 변경 될 때 호출하는 아약스를 얻는 방법입니다 (이상적인 방법 일 수 있습니다) 또는 1 - 5 초마다 말할까요? setInterval을에서

$(function() { 

    $.ajax({ 
     type: "GET", 
     url: 'http://open.api.ebay.com/shopping?callname=GetMultipleItems&responseencoding=JSON&appid=&siteid=3&ItemID=350720045158,390524810753,251237014268,200902751277,140927144371&version=811', 
     dataType: "jsonp", 
     jsonp: "callbackname", 
     crossDomain : true, 
     data: { }, 
     success: function (result) {  

      arrayOfData = new Array(); 

      var items = result['Item']; 

      $.each(items, function(i, item) {     
       var title = items[i]['Title']; 
       var price = items[i]['ConvertedCurrentPrice']['Value']; 
       var timeleft = items[i]['TimeLeft'];                   
       arrayOfData.push(['400', title + '<br/><br />' + timeleft]);     
      }); 

      $('#graph').jqBarGraph({ 
       data: arrayOfData, 
       height: 800, 
       width: 1200, 
       barSpace: 20, 
       colors: ['#085497','#74b3ea'], 
       prefix: '£' 
      });          

     }, 
     error: function (data) { 
      console.log(arguments); 
     } 
    }); 

}); 
+0

새 JSON을 요청하지 않고도 eBay의 데이터가 변경되었는지 알 수 없습니다. karaxuna가 제안한대로 유일한 방법은 시간 기준입니다. – bgusach

+0

알겠습니다. 그래서 5 초마다 새로운 json을 확인합니다. 다시로드하지 않고 어떻게 차트에 반영 할 수 있습니까? –

+0

간단합니다. 이전 객체를 저장하고 새 객체가 올 때 중요한 속성이 다른지 확인하고, 그렇지 않은 경우 렌더링하십시오. – bgusach

답변

3

장소 아약스 요청 : 당신은 단지 가격이 변경되었을 때 반응 할 경우

setInterval(function(){ 
    //ajax request here 
}, 5 * 1000); 
+0

아약스 부분에서 작동합니다. 그러나 매번 차트가 다시로드됩니다. 차트를 증가시킬 수있는 방법이 있습니까? 이 http://workshop.rs/jqbargraph/을 사용하고 있습니다. –

0

, 당신은 같은 것을 할 수 있습니다. 매우 거친 내용이지만 방금 지침을 보여 드리고자합니다.

$(function() { 

var last_price = {};  // object to keep track of previous prices 

// factored out function 
var update_chart = function (data_to_be_plotted) 
{ 
    $('#graph').jqBarGraph({ 
     data: data_to_be_plotted, 
     height: 800, 
     width: 1200, 
     barSpace: 20, 
     colors: ['#085497','#74b3ea'], 
     prefix: '£' 
    });          
}; 

$.ajax({ 
    //...bla bla bla... 

     success: function (result) {  

      arrayOfData = new Array(); 

      var items = result['Item']; 

      $.each(items, function(i, item) { 

       var title = items[i]['Title']; 
       var price = items[i]['ConvertedCurrentPrice']['Value']; 

       // if this item was not registered before, or the price is different 
       if (! last_price[title] || last_price[title] !== price) 
       { 
        // this you have to adapt to your own needs 
        arrayOfData.push(title + '<br/><br />' + price); 
       } 
       // register the price for the item, so that you know the next time 
       last_price[title] = price; 
      }); 

      if (arrayOfData.length > 0) // i.e.: something new came 
      { 
       update_chart(arrayOfData); 
      } 

    }); 

});