简体   繁体   中英

How to create a call back function and store the value of the variable

I am reading a Rss feed using setInterval method and displaying notification to the users ,I want to make sure I store the latest feed title so that the user does not get multiple notification of the same title again. The current implementation does not work because I cant use that variable until the response comes back. To make things worse I am delaying the execution.So I am guessing I need to use callback function get the value and do my checking inside that function. I am not able to figure out how to do the callback and get the value of entry_title.

/** global variable **/

var global_Rsstitle;

/** end global variable **/

function get_rss1_feeds() {

    var Rss1_title = getRss("http://rss.cnn.com/rss/cnn_topstories.rss", function(entry_title) {
        if(global_Rsstitle != entry_title)
        global_Rsstitle = entry_title;
        console.log('test',global_Rsstitle); // the value is outputed but global var is not working
    });
console.log('test1',global_Rsstitle);   // outputted as undefined ??
    }

    google.load("feeds", "1");
    google.setOnLoadCallback(function () { setInterval(get_rss1_feeds, 5000); });

My jsRss.js

function getRss(url, callback){
    if(url == null) return false;

    // Our callback function, for when a feed is loaded.
    function feedLoaded(result) {
        if (!result.error) {
            var entry = result.feed.entries[0];
            var entry_title = entry.title; // need to get this value
            callback && callback(entry_title);        
        }
    }
    function Load() {       
        // Create a feed instance that will grab feed.
        var feed = new google.feeds.Feed(url);
        // Calling load sends the request off.  It requires a callback function.
        feed.load(feedLoaded);      
    }    
    Load();             
}

can u see the entry_title -> this stores d value i need so i need to get this value n store it into a global variable or send it to another fns as a argument so that I can maintain the value and when next time setInterval is fired I get a new value so I can compare and check if its same n if its same I dont display it to the user

google.load("feeds", "1");
google.setOnLoadCallback(function () {
    var oldTitle = '',
        newTitle = '',
        getRss = function (url, callback) {
            (url) && (function (url) {
                var feed = new google.feeds.Feed(url);

                feed.load(function (result) {
                    (!result.error && callback) && (callback(result.feed.entries[0].title));
                });
            }(url));
        };

    setInterval(function () {
        getRss(
            'http://rss.cnn.com/rss/cnn_topstories.rss',
            function (title) {
                newTitle = title;
                if(oldTitle !== newTitle) {
                    oldTitle = newTitle;
                    console.log('oldTitle: ', oldTitle);
                }
                console.log('newTitle: ', newTitle);
            }
        );
    }, 5000);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM