I have difficulty understanding how a javascript callback should work. I am using the following code to get values from a MySQL database:
$.post("getResults.php", { x: 100, y: 200},
function(arrayOfValues){
alert( arrayOfValues[2] )
}, 'json');
alert()
returns the correct value, and is functioning as expected. However, I don't understand how the value from arrayData[2]
can be passed or returned to other parts of my program (and attempts I've made have not worked).
I've read through the jquery help and numerous SO questions and answers but I don't understand how a callback works.
In this case, I want arrayOfValues[2] to be accessible by my main program.
There are many ways to handle the returned data.
Set a variable with scope outside of that inline function and pass the value to it.
var result;
in callback
result = arrayData;
You can also invoke a function to do other things with the result.
The response data will be stored into the
foo
variable. Note, however, that this is an asynchronous callback function which is called when the HTTP response arrives. It is important to understand that you cannot use thefoo
variable until that HTTP response has arrived and the callback function has finished.The $.post() is asynchronous (non-blocking) call, which means that the function returns immediately (does not wait for server response) and next javascript code is executed. When response from the server arrives, your callback function is called, with the response...
Let's say, you want to display the content in a div:
Or you can have an object with some logic and register object's method as a callback:
Basically, you have many options. So if you need more info, specify what exactly you wanna do the response...
Callbacks, let me try and explain it in a way that you can understand.
This allows you to pass around functions and only executed them when you want, heres an example how I can pass a function to a variable and not executing it:
This is a small example and most common, you would call the function like
a()
adding the()
tells the function to execute.the below example shows you how you can pass a function into the above function
a
whilst executing it.an anonymous function is not assigned at the point of writing it but is assigned when its parsed within the outer functions scope.
the above my be a little tricky to understand but hopefully the example will clear it up.
The above is the same as the
b(a)
example apart froma
is now an anonymous function, and is only assigned to the variable withinb
, i.e the outer function.jQuer'ies API relies heavily on anonymous functions as there easy to use and create separation from the jQuery Core and the users code, the user does not need to heavily rely on the main code of jQuery but pass a callback to allow jQuery to connect to your code rather.
an example of a function that uses similar means of passing data in and out of functions are like so:
the above example only shows you roughly how the style of jQuery works, but not the most important benefit of callbacks and that's Ajax.
Ajax can halt a javascript script until its data has been received, for example:
when Ajax sees this line it has to wait for
RequestPage
to return the result because the coding further down is expected to require this value, so we have to wat for the data to be returned from the server before JavaScript continues, this can be several seconds and cause the UI To act abnormally.The way we get around this is to send a callback to the function and tell it to call when its done as I want to work on the rest of the application.
so now the JavaScript continues down the page performing the task it needs, and within the
RequestPage
function it would be layed out like so:This way the rest of your application has done other task and when the result is ready you can change the UI froim the result.
This is also why we use loaders to replicate a working state.
I hope you begin to understand how jQuery takes advantage of these factors of javaScript and it will help you to develop your application.