This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I have this code
var base_url = 'http://api.zxc.loc';
var questions = [];
function get_question(){
if(!questions.lenght){
$.get(base_url + '/api/questions', function(data){
questions = data;
console.log('get_question', questions); // GOOD, return [Object, Object ... etc ... ]
}, 'json')
}
console.log('get_question_2', questions); // BAD return empty array []
/* var question = questions[0];
delete questions[0];
return question; */
}
How you can see, in $.get questions variable not empty, but after $.get, questions is empty!
Why? Thanks
This is how AJAX works. In your code get_question_2 will be logged before get_question. Once an ajax call is made, the code proceeds past it (to get_question_2). Then one the ajax call is returned, the code inside your function (namely get_question) will be executed.
To summarize, the log with get_question_2 is being called as soon as the ajax call is finished being made and BEFORE the ajax call returns with the data.
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
I'm doing a GET request with jQuery inside of a function and trying to set a variable declared earlier in that function with the result. However, it comes up as undefined. Am I missing the concept here? How can I make something like this work? Thanks.
function doSomething1() {
var x;
$.get( window.location.href, { q: 'stuff', q2: $('input').val() }, function(data){
// value shows up
console.log(data);
x = data;
});
return x;
}
function doSomething2() {
// comes up as undefined.
console.log(doSomething1());
}
doSomething2();
This is a fault of the asynchronous effect of $.get() requests. You are returning x before the get has a chance to do anything. It takes a while to wrap your head around async functions.
The order of events is as follows:
doSomething2() calls doSomething1()
doSomething1() defines x without a value begins the GET request, and returns undefined.
doSomething2() logs the returned x value
The GET request finishes and processes its success function
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
Here is an over view of my js file where I am calling 2 function foo1 and foo2.
function foo1(){
// do some ajax call which takes random amount of time
}
function foo2(){
// do something which needs foo1() being already done
}
foo1();
// reach here only if foo1 is complete
foo2();
ps. I dont want to use jQuery or knockout.
Also have already looked into How to wait for a func...
Given you say foo1() is doing ajax, you'd need something like this:
function foo1() {
$.ajax( ....
success: function() { foo2(); }
);
}
function foo2() {
console.log("foo1() finished");
}
foo1();
So when the ajax call returns (successfully), it'll automatically call foo2(), and the only thing you need to do is call foo1().
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
This is probably a crazy stupid question, but I don't get why the things variable is not being reset. Here's what I have:
var things = "";
jQuery.get("/Users/MyName/Documents/File.txt", function(data) {
things = data;
});
console.log(things)
Whenever I do the console log within the function, it works. When I take it out of the function, it doesn't work. I thought I was setting the things var globally but apparently not?
var things = "";
jQuery.get("/Users/MyName/Documents/File.txt", function(data) {
things = data;
}).done(function() {
console.log(things);
});
is same as
var things = "";
jQuery.get("/Users/MyName/Documents/File.txt", function(data) {
things = data;
console.log(things);
});
Passing a function as second param into $.get, it will be called when AJAX request is succesfull(aka status = 200);
If you want to use it outside, your have to use done() method, and first param(a function) is called if successfull, second on error and third is always.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 years ago.
Im having an issue assigning a string to a variable and then returning it. It sounds simple and should be but im completely lost as to why what is happening is happening.
My code:
function drawpills() {
var picid;
$.post('js/fetchdata.php', function (data) {
var clock = document.getElementById('clock');
clock.innerHTML = "<img src='images/clock/pill.png' alt='pill_image' id='pillpic" + data + "'/>";
picid = "pillpic" + data;
alert(picid); //if i run it here i get pillpic31 which is what i want
});
alert(picid); //if i run it here i get undefined which is not what i want and which is what is being returned
return picid;
}
I highlighted the issue with comments on the respective line. Any help is appriecated.
Suggest you read through the linked article in comments.
What you need is something like this:
function drawpills(callback){
var picid;
$.post('js/fetchdata.php', function(data){
var pcid = data; // process pcid here
callback(pcid);
});
}
The problem is the asynchronous behavior of AJAX. When the AJAX response is sent, it moves onto the next statement, whether or not the call has finished executing. You can now do one of two things. You could either make the AJAX synchronous, which will probably break the timing of events on the page, or you could create a callback to be executed when the AJAX call is completely finished.
$.post callback is asynchronous function,so:
function drawpills(){
var picid;
var callback = function(data){
var clock = document.getElementById('clock');
clock.innerHTML="<img src='images/clock/pill.png' alt='pill_image' id='pillpic"+data+"'/>";
picid="pillpic"+data;
alert(picid);
}
$.post('js/fetchdata.php', callback);
//picid is undefined,because callback is running until server response data.
//unless you using synchronous request
alert(picid);
return picid;
}
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 years ago.
I'm trying to use $.get() to retrieve data from a server, and assign it to a variable to work with it. For example:
$.get('htmllink', {}, function(results){
//do stuff here
});
var morestuff = ; //$.get() data goes here
The problem is, I can't get anything to work outside the rounded brackets. Everything is treated as internal variables or something. What am I missing?
You need to do all of your processing inside the callback function. This is because the call to $.get only starts an asynchronous request, and then the next line (var morestuff = ...) is executed after the request starts, but before it has completed.
$.get('htmllink', {}, function(results){
//do stuff with results in here
});
The problem here is that the $.get request is asynch (Ajax), so there is a timing issue here. The
var morestuff =
will run before the Ajax call returns, so you won't have the value to assign.
You have to interact with the result of the ajax request in the call back to have access to it
$.get('htmllink', {}, function(results){
//all code that depends on results must run inside here
});
//you can't execute code here that depends on the Ajax call