First of all I've read this thread (especially this answer)about async calls and returns. However I still struggeling with my issue for a few days now!
I'm building a script to check if a postcode is available in an array. The answer should be true
or false
. However using that thread's answers still keeps returning undefined
. I'm still learning so be gentle :)
What I've done is this:
function checkPostcode(pc_nr_check) {
...........
$.getJSON(url+'?format=json', function(data){
........
var postcodeArray = postcodes
var postcodeInArray = pc_nr_check
if ($.inArray(postcodeInArray, postcodeArray) > -1){
myCallback(true)
} else {
myCallback(false)
}
});
}
function myCallback(response) {
var answer = response;
return answer;
} // console.log this give true or false
$('.custom-checkout').on('click', function(e){
var is_available = myCallback();
if(is_available){
// show some stuff
}
});
I just can't see why this is undefined. I also tried to set a global var answer
and removed the return answer;
from the myCallback
function.
Any help greatly appreciated.