I need to reference clues in a function later on in my code. How can i reference clues outside of the .git method?
$.get("clues.txt", function(data)
{
var clues = data.split(',');
});
I need to reference clues in a function later on in my code. How can i reference clues outside of the .git method?
$.get("clues.txt", function(data)
{
var clues = data.split(',');
});
You can't reliably access clues
outside the specific callback where its value is supplied. Because the result is obtained via an asynchronous operation, the timing of the completion of that asynchronous operation is unknown. Thus, the only place you can reliably use the result is inside the callback itself or in a function you call from within that callback and pass the value to. This is how you do asynchronous programming. You continue your programming sequence from within this callback.
$.get("clues.txt", function(data) {
var clues = data.split(',');
// other code goes here that uses the `clues` variable
});
// code here cannot use the `clues` variable because the asynchronous operation
// has not yet completed so the value is not yet available
Here are some other related answers:
How do I get a variable to exist outside of this node.js code block?
As per the reference of Store ajax result in jQuery variable. you can pass your response data to some further function. Moreover, you can also play with it by storing your response into your some hidden
HTML input tag.
<input type="hidden" id="clues_data" value="">
So, in your .get()
method you can do something like this:
$.get("clues.txt", function(data)
{
$("#clues_data").val(data);
});
And then in your some further function you can access it like:
function someFurtherFunction(){
var clues = $("#clues_data").val().split(',');
// do something with clues
}
I know this is not the exact solution for your question, but I've tried to help you handle this situation. It may help some-other coder :)