This script uses an API to access information from a database and displays a 'spotlight' for a random person in the db that includes their name, a photo, and a short bio.
I'm trying to write this script to check if the image exists. If the image exists, then the script should continue, but if the image doesn't exists (404 error) it should reroll and check again. I'm having issues understanding how to work with the asynchronous part of ajax.
function checkImage(imgurl){
function headRequest(url){
return $.ajax({ //Makes a HEAD request for the image
url: url,
type: 'HEAD'
});
};
headRequest(imgurl)
.done(function(){
return true; //image exists
})
.fail(function(){
return false; //HEAD request returns 404
});
};
//if image does not exist or person.biography is undefined, reroll
while (!checkImage(imgsrc) || person.biography == undefined) {
//select random person
}