Javascript 当JSON发生变化时,如何更新

Javascript 当JSON发生变化时,如何更新,javascript,json,ajax,ebay-api,jqbargraph,Javascript,Json,Ajax,Ebay Api,Jqbargraph,我已经构建了一个简单的web应用程序,它从ebay API获取一些数据(JSON),并将结果绘制到一个图表上,显示每种商品的价格。这很有效 然而,我希望图表在实时更新,如果一个项目得到了出价或完成,例如。所有这些数据都包含在从ebay返回的JSON中 我的问题是,当JSON发生变化时(这将是理想的方法),或者说每隔1-5秒,如何更新图形和调用ajax $(function() { $.ajax({ type: "GET", url: 'http://op

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

然而,我希望图表在实时更新,如果一个项目得到了出价或完成,例如。所有这些数据都包含在从ebay返回的JSON中

我的问题是,当JSON发生变化时(这将是理想的方法),或者说每隔1-5秒,如何更新图形和调用ajax

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

 });
$(函数(){
$.ajax({
键入:“获取”,
网址:'http://open.api.ebay.com/shopping?callname=GetMultipleItems&responseencoding=JSON&appid=&siteid=3&ItemID=350720045158,390524810753251237014268200902751277140927144371&version=811',
数据类型:“jsonp”,
jsonp:“callbackname”,
跨域:是的,
数据:{},
成功:函数(结果){
arrayOfData=新数组();
var items=结果['Item'];
$。每个(项目,功能(i,项目){
var title=项目[i]['title'];
var价格=项目[i]['ConvertedCurrentPrice']['Value'];
var timeleft=项目[i]['timeleft'];
arrayOfData.push(['400',title+'

'+timeleft]); }); $(“#图”).jqBarGraph({ 数据:arrayOfData, 身高:800, 宽度:1200, 酒吧空间:20, 颜色:['#085497','#74b3ea'], 前缀:‘’ }); }, 错误:函数(数据){ log(参数); } }); });
将ajax请求放在setInterval中:

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

如果你只是想在价格变化时做出反应,你可以这样做。这很粗糙,但我只想给你看一个指南:

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

    });

});
$(函数(){
var last_price={};//跟踪以前价格的对象
//分解函数
var更新图表=功能(绘制数据)
{
$(“#图”).jqBarGraph({
数据:要绘制的数据,
身高:800,
宽度:1200,
酒吧空间:20,
颜色:['#085497','#74b3ea'],
前缀:‘’
});                                      
};
$.ajax({
//……呜呜呜呜。。。
成功:函数(结果){
arrayOfData=新数组();
var items=结果['Item'];
$。每个(项目,功能(i,项目){
var title=项目[i]['title'];
var价格=项目[i]['ConvertedCurrentPrice']['Value'];
//如果该商品之前未注册,或价格不同
如果(!last_price[标题]| last_price[标题]!==价格)
{
//你必须适应自己的需要
arrayOfData.push(标题+价格);
}
//登记物品的价格,以便下次知道
最后价格[标题]=价格;
});
if(arrayOfData.length>0)//即:出现了新的内容
{
更新图表(arrayOfData);
}
});
});

如果不请求新的JSON,你就无法知道易趣上的数据是否发生了变化。唯一的方法是按照karaxuna的建议按时进行。好吧,我理解。那么,假设我每5秒钟检查一次新的JSON。我如何在不再次加载的情况下将其反映在图表上?这很简单。存储旧对象,当新对象出现时,检查重要属性是不同的,如果是,则渲染它。你能把它作为一个答案吗?是的,我可以。但我在想:你真的想这样做吗?据我所知,你也会渲染剩余的时间,并且随着时间不断刷新图表会很好。这对ajax部分很有效。但它每次都会重新加载图表。是否有一个w我可以增加图表吗?我在用这个