There are several ways to achieve this. You obviously need to wait until the function call A () JSON is complete before functionB () is executed. One way to do this is to call function B () from within the success () handler in a call to function A () JSON, for example:
function functionA() { $.getJSON(url, data, function(result) {
If you do not want to hard code the function function inside function A, you can also pass it as an argument and call it this way, for example:
function functionA(callback) { $.getJSON(url, data, function(result) {
Another, more elegant and scalable solution is to let the function call A () JSON fire a custom event when it is created and create a new event handler for that particular event, which then fires function B. Thus, function A and function B remain completely decoupled, you do not need to know each other, but you can still ensure the correct execution order. You can read about triggering and selecting custom events here: jQuery.trigger
source share