2

ebay APIからいくつかのデータ(JSON)をフェッチし、その結果を各アイテムの価格を示すグラフにプロットする単純なWebアプリを作成しました。これはうまく機能します。

ただし、たとえば、アイテムが入札された場合や終了した場合は、チャートをリアルタイムで更新したいと思います。このデータはすべて、ebayから返されたJSONに含まれています。

私の問題は、JSONの変更時にグラフを更新してajaxを呼び出す方法(これが理想的な方法です)または1〜5秒ごとに呼び出す方法です。

$(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);
        }
    });

 });
4

2 に答える 2

3

setIntervalにajaxリクエストを配置します。

setInterval(function(){
    //ajax request here
}, 5 * 1000);
于 2013-03-07T17:26:48.203 に答える
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);
            }

    });

});
于 2013-03-08T12:13:07.880 に答える