I have two functions. one function call for the ajax function. My problem is it is not waiting until the ajax function is complete. it is executing my other code before. How to wait for ajax is complete?
function getdata(){
var imgPath = getImgPath();
const img2 = document.getElementById("backimg");
img.src = imgPath;
}
function getImgPath(){
$.ajax({
url:spath,
type:'HEAD',
dataType: "jsonp",
crossDomain: true,
secure: true,
// async:false,
headers: {
"accept": "application/json",
"Access-Control-Allow-Origin":"*"
},
success: function()
{
//file exists do something here
console.log("exist ");
path = spath;
},
error: function()
{
//file does not exist
console.log("doesnt exist");
path = lpath;
}
});
}
This is what I tried. This ajax function executes well but when I run my code before finish the getImgPath() function it is executing other code lines. How can I wait until the ajax function is finished?