捕获新请求时如何检查JSON对象值是否已更改 [英] How to check if a JSON object value has changed when new request is captured

查看:129
本文介绍了捕获新请求时如何检查JSON对象值是否已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php脚本,可以JSON格式获取特定用户的最新推文. JSON被解析为AJAX函数,该函数在页面上的ID中显示最新的tweet.每x秒设置一个计时器以执行上述操作,无需用户刷新页面即可检索最新的tweet.这完美地工作.

我现在正在尝试创建一个通知功能,当有新的推文要显示时显示通知.每个推文在检索到的JSON中都有一个唯一的ID,称为"id_string".我想要做的是以某种方式存储生成的id_string的值,然后每次发出请求以检索新的tweet时,针对存储的t_et检查新的"id_string",如果不同则显示通知.

关于如何实现此目标的任何想法都很棒?!我已经研究了本地存储,但是我对此并不十分熟悉,据我所知,您无法对照本地存储中的其他字符串来检查字符串.

以下是帮助解释我的问题的必要代码:

PHP向Twitter发出请求并以JSON格式(tweets.php)生成输出:

require_once('config/FrameFunctions.php');

$output = array();

foreach ($tweeters as $i => $tweeter){
    $theTweeter = new Tweeter($tweeter, $tmhOAuth);
    $allinfo = $theTweeter->getTweets();
    $output[$i] = $allinfo;
}

header("Content-type: application/json");
echo json_encode($output);

每隔x秒发出请求并生成客户端输出的javascript:

$(document).ready(function() {

   $('.fancybox').fancybox();
   /*
   *  call ajax function and update latest
   */        

    var refreshTweets = function() {
      console.log("updating..");
      $.ajax({url:"tweets.php",success:function(result){
        tweets = eval(result);

        for(i=0;i<tweets.length;i++){
            $("#latesttweet"+(i+1)).html(
               tweets[i][0].user.name + ": " + tweets[i][0].text
             );  
        }

      }});

    }
    refreshTweets();

    //set the time in milliseconds here for each refresh
    setInterval(refreshTweets , 30000); //Interval

});

解决方案

这是基于我上面的评论的解决方案

/*
NOTE : allTweets will have to be populated with all the tweets ids on the page prior           
to being called in refreshTweets so that it does not trigger fake notifications
*/

    var allTweets = Array();

    $(document).ready(function() {

        $('.fancybox').fancybox();
        /*
         *  call ajax function and update latest
         */        

        var refreshTweets = function() {
            console.log("updating..");
            $.ajax({url:"tweets.php",success:function(result){
                    tweets = result;

                    for(i=0;i<tweets.length;i++) {

                        //check if its a new tweet or not
                        if (allTweets[tweets[i][0].id_string] === undefined) {
                            allTweets[tweets[i][0].id_string] = 1;    
                            //trigger notification here!
                        }

                        $("#latesttweet"+(i+1)).html(
                        tweets[i][0].user.name + ": " + tweets[i][0].text

                    );  
                    }

                }});

        }
        refreshTweets();

        //set the time in milliseconds here for each refresh
        setInterval(refreshTweets , 30000); //Interval

    });

I have a php script that gets the latest tweet of particular users in JSON format. The JSON is parsed into an AJAX function that displays the latest tweet in an ID on the page. A timer is set for every x seconds to execute the above, retrieving the latest tweet without the user having to refresh the page. This works perfectly.

I am now trying to create a notification feature that displays a notification when there is a new tweet to display. Each tweet has a unique ID called 'id_string' in the JSON retrieved. What I want to do is somehow store the generated id_string's value, and then each time a request is made to retrieve a new tweet, check the new 'id_string' against the stored one, and if it is different then display the notification.

Any ideas on how to go about this would be great?! I've looked into local storage but I'm not very familiar with this, and as far as I'm aware you can't check strings against other strings in local storage.

Here is the necessary code to help with interpreting my question:

PHP to make request to twitter and generate output in JSON format (tweets.php):

require_once('config/FrameFunctions.php');

$output = array();

foreach ($tweeters as $i => $tweeter){
    $theTweeter = new Tweeter($tweeter, $tmhOAuth);
    $allinfo = $theTweeter->getTweets();
    $output[$i] = $allinfo;
}

header("Content-type: application/json");
echo json_encode($output);

The javascript to make the request every x seconds and generate the client output:

$(document).ready(function() {

   $('.fancybox').fancybox();
   /*
   *  call ajax function and update latest
   */        

    var refreshTweets = function() {
      console.log("updating..");
      $.ajax({url:"tweets.php",success:function(result){
        tweets = eval(result);

        for(i=0;i<tweets.length;i++){
            $("#latesttweet"+(i+1)).html(
               tweets[i][0].user.name + ": " + tweets[i][0].text
             );  
        }

      }});

    }
    refreshTweets();

    //set the time in milliseconds here for each refresh
    setInterval(refreshTweets , 30000); //Interval

});

解决方案

here is a solution based on my comments above

/*
NOTE : allTweets will have to be populated with all the tweets ids on the page prior           
to being called in refreshTweets so that it does not trigger fake notifications
*/

    var allTweets = Array();

    $(document).ready(function() {

        $('.fancybox').fancybox();
        /*
         *  call ajax function and update latest
         */        

        var refreshTweets = function() {
            console.log("updating..");
            $.ajax({url:"tweets.php",success:function(result){
                    tweets = result;

                    for(i=0;i<tweets.length;i++) {

                        //check if its a new tweet or not
                        if (allTweets[tweets[i][0].id_string] === undefined) {
                            allTweets[tweets[i][0].id_string] = 1;    
                            //trigger notification here!
                        }

                        $("#latesttweet"+(i+1)).html(
                        tweets[i][0].user.name + ": " + tweets[i][0].text

                    );  
                    }

                }});

        }
        refreshTweets();

        //set the time in milliseconds here for each refresh
        setInterval(refreshTweets , 30000); //Interval

    });

这篇关于捕获新请求时如何检查JSON对象值是否已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆