I’m trying to create a variable from the result of a jsonp request, and the use this variable later in the script. But it seems like the part where the variable is used is triggered before the jsonp request is done.
function onSuccess(data) {
var result = data['result'];
console.log(result);
}
function onError(data) {
alert(data);
}
$.ajax({
url:"http://suneeriksen.unboxer.org/",
cache: false,
dataType: 'jsonp',
jsonp: 'callback',
timeout: 5000,
success: onSuccess,
error: onError
});
var gallery,
el,
i,
page,
dots = document.querySelectorAll('#nav li'),
slides = result //this is where I try to fetch the variable
];
I’ve tried to figure out how this could be done. I’ve tried to put the jsonp request in a function and done like this:
var slides = jsonpResult();
Any clue what to do?
EDIT:
I also have functions using the gallery variable
gallery.onFlip(function () {
//Doing stuff
}
And I can’t put all of these in the onSuccess function.
There are two ways:
Make the AJAX call not A(synchronous) by setting
async: trueat the ajax call options (please note that this approach is now deprecated).or:
do call whatever code that uses the
slidesvariable from inside the success callback (and do the assigning there as well, if you need it for future use).