I have next function that return me the AJAX responce or default false
value:
function getResults() {
// get json object from the database
var json = false // default value
var data = new FormData()
var xhr = new XMLHttpRequest()
xhr.open('POST', "php/get_survey_results.php", true)
xhr.onload = function(){
var jsonString = this.responseText
json = JSON.parse(decodeURIComponent(jsonString))
return json
};
xhr.send(data);
return json
}
function foo() {
var data = getResults() // returns false, because AJAX responce gets later
// processing data...
}
AJAX responce is coming later on +- 300 milliseconds and that is why getResults
returns false. I want it to wait until AJAX responce will be get or wait some seconds before returning false if not result get. How could I do it ?
I need default value becaue if I remove it - next code in foo
will be executed but data
variable will be empty