I posted a question that was marked as duplicate as this got me doing some research into posting return true;
inside a nested function. I now understand about asynchronous code but I'm still having big difficulty coding a solution.
The code that was originally causing me problems, which I was using to submit a form inside an on("submit") function, is:
if($('input[name="lat"]').length){
// submit the form after checks have been made
return true;
} else {
e.preventDefault();
geocode_clientside();
setTimeout(function () {
return true;
}, 1000); // in milliseconds
}
After much reading I got to this:
var result = false;
if($('input[name="lat"]').length){
// submit the form after checks have been made
result = true;
} else {
e.preventDefault();
geocode_clientside();
setTimeout(function () {
result = true;
// console log just to help me with debugging
console.log('settimeout: ' + result);
}, 1000); // in milliseconds
}
// console log just to help me with debugging
console.log('end: ' + result);
return result;
This isn't working, obviously, because return result;
is returning "false" when input[name="lat"] has no length. I only understood this when I coded my new solution, and it feels like I've run a big circle and come back to the start of my problem. I fully understand now why it's returning false, what I don't understand is the solution that will get me to the end (without returning to the beginning).
I tried something stupid, which was to wrap the return result; in a delay function, which was a disaster.
Can anyone help me here, please?