The stuff I don´t understand I find it rather long to explain so please bear with me. I have the following incude js in my page:
var include = (function(){
var exports = {}
var test = function(url){
var result = [];
$.ajax(url).then(function(data){
result = data.form.records; // assume data.form.records valid
console.log(result) // displays correctly Array [Object,Object]
});
return result;
}
exports.test = test;
return exports
})();
then from the page i make the following call (assum that some_url is valid)
var array = include.test(some_url);
later i have a event ( some button click ) that will use the values in the array
input.on('click',function(){
console.log(array) // 2
});
My problem is that when the event occurs the array variable is still VOID , The console.log // 2 displays Array [ ]....
i know my test function returns immediately so the immediate value of array variable is Array [ ] , but by the time the input.click gets called , the then() callback would already have been executed so the array itself should be modified .After all array is a reference ...
Any suggestion ? I don´t usually ask questions because most of the time i find the answers by searching but i already tried for a day. Maybe i have a lack of inspiration. Some help would be appreciated