2

我已经构建了一个简单的 Web 应用程序,它从 ebay API 获取一些数据 (JSON),并将结果绘制到显示每件商品价格的图表上。这很好用。

但是,例如,如果某个项目得到投标或完成,我希望图表能够实时更新。所有这些数据都包含在从 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

将 ajax 请求放在 setInterval 中:

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 回答