I'm having difficult to understand the $.get call inside a loop . On the example below I call a $.get inside a function (The file that i'm reading on $.get doesn't matter for this problem), and it seems to be read just after all the first loop is finished.
I expect console to show:
---
1
---
2
---
3
---
But is showing
1
2
3
4
5
6
7
8
9
10
---(11)
Can you tell me why?
http://jsfiddle.net/qh6zn514/1/
<Button id="clickme">Click me</button>
$("#clickme").click(function() {
for ($i=0;$i<=10;$i++)
{
readNumber();
console.log($i);
}
})
function readNumber () {
$.get ("file", function(txt) {
console.log ("---");
});
}