0
$.getJSON("/data/data/com.android.tcptest/files/BPL.json", function(data) {
        var i = 0, dataSize = data.length, tmpArray = [];
        for(i; i < dataSize; i++){
            tmpArray.push ([data[i].Time.substring(0, data[i].Time.length -2).replace(/:/g, ""),data[i].Bid ]);
        }
        return tmpArray;
    });
    $.plot($("#chart"), [ tmpArray ]);  

How can I use tmpArray outside getJSON() function. My programme doesn't work. Thanks!

2
  • 2
    define it somewhere else outside getJSON Commented Aug 2, 2011 at 15:00
  • This question is similar to: How do I return the response from an asynchronous call?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 8, 2024 at 22:11

3 Answers 3

1

Initialize tmpArray outside of the getJSON block.

Edit: Quentin is right, the array would be empty. You'll also need to make the request synchronous. I wrote the following example, hope it helps: http://jsfiddle.net/mXHfC/

Sign up to request clarification or add additional context in comments.

3 Comments

Could you should me how to do that?
Then you'll just be passing an empty array to plot.
Could u tell me how to do that? I tried "tmpArray =[]" outside getJSON but still fail.
0

Asynchronous JavaScript and XML is asynchronous.

You can't return from it, any more then you could return from:

$('button').click(function () {
    return tmpArray;
});
$.plot($("#chart"), [ tmpArray ]);  /* Does this method really expect an array consisting of a single value that is another array? */

The code in the callback won't execute until after the HTTP response has arrived (or the button has been clicked).

Do the work inside the callback.

$.getJSON("/data/data/com.android.tcptest/files/BPL.json", function(data) {
    var i = 0, dataSize = data.length, tmpArray = [];
    for(i; i < dataSize; i++){
        tmpArray.push ([data[i].Time.substring(0, data[i].Time.length -2).replace(/:/g, ""),data[i].Bid ]);
    }
    $.plot($("#chart"), [ tmpArray ]);  
});

1 Comment

I tried this before, this work in brower but android. Put plot outside function works on android but only with some stastic values. like [[1,2],[2,3],[4,5]]
0

getJSON is asynchronous, you can not use the array before the function is returned!

$.getJSON("/data/data/com.android.tcptest/files/BPL.json", function(data) {
        var i = 0, dataSize = data.length, tmpArray = [];
        for(i; i < dataSize; i++){
            tmpArray.push ([data[i].Time.substring(0, data[i].Time.length -2).replace(/:/g, ""),data[i].Bid ]);
        }
        $.plot($("#chart"), [ tmpArray ]);  
        //return tmpArray;
    });

2 Comments

This is working fine with brower but when I put this into android--->failed. I tried to put plot outside getjson and give it some specific value(like[[1,2],[3,4],[5,6]]). It works. Confuse!!!
Are you sure the android device is calling the success function? Is it creating an array correctly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.