how to get the value from a callback function

I am relatively new to javascript and I am facing some difficulty.I have two java script files as I have shown below. I am having trouble getting the value of the variable entry_title inside the getRss function and storing it inside the variables Rss1_title and Rss2_title . Creating a global variable and assigning it to entry_title will make things worse as I will not be able to know from which Rss url the title came from. Is there a easy way to get the value of the callback functions ?

<script type="text/javascript" src="jsRss.js"></script>
<script type="text/javascript" src="notification.js"></script>

My notification.js file

    function get_rss1_feeds(){
        var Rss1_title = getRss("http://yofreesamples.com/category/free-coupons/feed/?type=rss");
    }

    function get_rss2_feeds(){
        var Rss2_title = getRss("http://yofreesamples.com/category/real-freebies/feed/?type=rss");  
    }
setTimeout('get_rss1_feeds()',8000);
setTimeout('get_rss2_feeds()',7000);

My jsRss.js file:

function getRss(url){

    if(url == null) return false;

    google.load("feeds", "1");

    // 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           
        }

    }       
    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);              
    }
    google.setOnLoadCallback(Load);             
}

Errors :

When the setTimeout(get_rss1_feeds, 8000); method is called I get a blank screen. I get a error in my console saying octal literals and octal escape sequences are deprecated and it is pointing to the 6th line in this script. Is it because I am using google-api for parsing my Rss?

if (window['google'] != undefined && window['google']['loader'] != undefined) {
if (!window['google']['feeds']) {
window['google']['feeds'] = {};
google.feeds.Version = '1.0';
google.feeds.JSHash = '8992c0a2cdf258e5bd0f517c78243cd6';
google.feeds.LoadArgs = 'file\75feeds\46v\0751';
}
google.loader.writeLoadTag("css", google.loader.ServiceBase + "/api/feeds/1.0/8992c0a2cdf258e5bd0f517c78243cd6/default+en.css", false);
google.loader.writeLoadTag("script", google.loader.ServiceBase + "/api/feeds/1.0/8992c0a2cdf258e5bd0f517c78243cd6/default+en.I.js", false);
} 

Answers:

Answer

Seeing as it's a different scope, you can either return it in a callback, or provide it in another way such as exporting it to a higher scope that is visible to your desired location. In this case, it's the global scope, so I'd advise against that.

function getRss(url, callback) {
//...
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);        
    }
}

and call it like so,

function get_rss1_feeds() {
    var Rss1_title = getRss("http://yofreesamples.com/category/free-coupons/feed/?type=rss", function(entry_title) {
        // This scope has access to entry_title
    });
}

As an aside, use your setTimeout like so:

setTimeout(get_rss1_feeds, 8000);

rather than

setTimeout("get_rss1_feeds()", 8000);

as the latter uses eval, whereas the former passes a reference to the function.

Answer

Eventhough it will make your code a mess, you can append the variables to the window object. For example:

function a()
{
 window.testStr = "test";

}

function b()
{
 alert(window.testStr);
}

Or even create your own object, instead of using window, as such:

var MyRSSReader = {
 TitleOne : '',
 TitleTwo : '' 
} 

MyRSSReader.TitleOne = "My title";

Wikipedia has a nice article about global variables, and why they are bad.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.