Reputation: 1157
I know that probably would be the dumbest question to ask, but its a desperate attempt from a UI guy to do something... i have getting some values from a json file, and then i am passing those values to plot a graph using jflot.
//Script
function plot4() {
$.getJSON("wc.json",function(data){
$.each(data.posts, function(i,data){
var wc = data.title;
alert(wc);
});
});
function drawPlot() {
alert(wc);
// function which draws plot using jFlot using a huge dataset
}
}
Is it okay that i wrap the getjson file outside the drawPlot function ??.. pls advice
Upvotes: 1
Views: 1516
Reputation: 3708
What's wrong with parameters?
function plot4() {
$.getJSON("wc.json",function(data){
$.each(data.posts, function(i,data){
var wc = data.title;
drawPlot(wc);
});
});
function drawPlot(wc) {
// function which draws plot using jFlot using a huge dataset
}
}
...or:
function plot4() {
$.getJSON("wc.json",function(data){
$.each(data.posts, function(i,data){
var wc = data.title;
drawPlot(wc);
});
});
}
function drawPlot(wc) {
// function which draws plot using jFlot using a huge dataset
}
Upvotes: 1
Reputation: 129529
No, because you declare wc
(via var wc
) to be in the scope of the "each" block.
To do what you want, you need to either:
move wc
out to the scope containing both functions
function plot4() {
var wc;
$.getJSON("wc.json",function(data){
$.each(data.posts, function(i,data){
wc = data.title;
Or, if you actually wish to call drawPlot
from your each
loop (you don't seem to be calling it all!), pass wc
as a parameter to it
// ... some other code
var wc = data.title;
drawPlot(wc);
// ... some other code
function drawPlot(wc) {
alert(wc);
// function which draws plot using jFlot using a huge dataset
}
Upvotes: 1