I have doing a website with javascript but I can't do that I want.
I have this
<script type="text/javascript">
res = new Array();
var fun1 = function () {
var control = $.Deferred();
for (i=0;i<5;i++) {
$.get("URL", function(data){
res[i]=data;
console.log ("i is: " + i + "and res is: " + res);
});
}
}
setTimeout(function () {
control.resolve();
}, 3000);
var show = function () {
console.log("Res finally is: " + res);
}
fun1().done(show);
</script>
I want to do a $.get with 5 or more different URL (I have a param in the URL) but I can't do it. res[i] is always the last element in the array (in this case is always res[5]=data and I want to fill the complete array, from 0 to 15.
First console.log always show
i is: 5 and res is: ,,,,,20
i is: 5 and res is: ,,,,,10
...
i is: 5 and res is: ,,,,,38
and the second console.log always return the last
Res finally is ,,,,,38
How can I do it correctly?
Thanks!