krishna_v
krishna_v

Reputation: 1511

d3.js return value of function always undefined

I am calling a function from my index.html file. The function is defined in a javascript file which i have referred to in the html. However the return value is always undefined. When i debug i could see the value in the return string.

Follwing is the code in index.html

<script type="text/javascript">
function readQueryStringparam(name) 
    {

        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.href);
        if (results == null)
        return "";
        else
        return results[1];
    } 


function getDiDataUrlPrefix() 
{

             diDataUrlPrefix = diGlobal.instanceInfo.getDiDataUrlPrefix();
            //alert(diDataUrlPrefix);   
            sbu = readQueryStringparam('sbu');
            appid = readQueryStringparam('appid');


            if (sbu.length > 0) 
            {
                sbu = sbu.trim();
                CreateChart(diDataUrlPrefix,sbu,0,appid);
            }
            else if (appid.length > 0)
                {

                    sbu = GetSBUForApplication(appid);
                    CreateChart(diDataUrlPrefix,0,0,appid);
                }
}

 </script>

I get the value for the parameters supplied in the url as well as diDataUrlPrefix.

Following is the code in the javascript file:

function GetSBUForApplication(appid)
{
    setTimeout(function() { }, 10000);
    var string;
    var file = diDataUrlPrefix + "/oss/csvs/Consolidated_RAG.csv";

    d3.text(file, function(datasetText) 
        {

            parsedCSVapp = d3.csv.parseRows(datasetText);


            if (appid >0)
            {
                    parsedCSVapp = parsedCSVapp.filter(function(row)
                    {
                        //alert(parsedCSVapp);
                        return row[0] == appid



                    })//parsed fileter ends here

                returnstring = parsedCSVapp[0][4];



            }

        })



    return returnstring;

}

However the value of sbu is always undefined.However i can see the values in parsedCSVapp. The csv file looks like this:

Application_Id,Application Name,Status,Name,Business Unit 200039,DEALING,RED,Marc Begun,Financial&Risk 200070,NGTX,RED,Marc Begun,Financial&Risk 200097,WORLD-CHECK,RED,Graham Fisher,Financial&Risk 200009,BOARDLINK,RED,Jennifer Simon,Financial&Risk 200088,THOMSON ONE,RED,Jonathan Weinberg,Financial&Risk 200037,DATASTREAM,RED,Ian Brocklehurst,Financial&Risk 200044,EIKON,RED,Olivier Martin,Financial&Risk 200011,COLLABORATION,RED,Frank Tarsillo,Financial&Risk

Upvotes: 0

Views: 1195

Answers (1)

Lars Kotthoff
Lars Kotthoff

Reputation: 109232

d3.text (and d3.csv, d3.json and similar) make asynchronous calls. That is, when you run the code, the call is made and execution resumes without waiting for the call to return. The second argument to those functions is a function that gets executed when the call returns -- the callback.

This function will not be executed at the same time as you run d3.text, but later. You cannot determine at what time exactly it will be run. Any code that you want to call as a result of one of those calls needs to be run as part of the callback function, or called from there.

Upvotes: 1

Related Questions