Doing some testing of a Jquery loop which calls a function within the loop 3 times. It seems like the function is called each time in the loop, but does NOT execute. At the end of the loop, the function then executes 3 times. Not what I want.
Why does the loadData not execute and append each time it is called? The alerts just tell me that each stage is performed as the loop cycles through.
My ultimate objective is to call a similar function each time until it fills the browser window to the bottom of the screen, then stop. But, with this behaviour, if I put a check in the loop for the condition of filled screen, it will not work since the screen will not fill at all as the loop churns.
Is there a way I can make this work, where a loop calls a function and the function actually executes each time it is called and do what it was designed to do?
Code below:
function loadData() {
alert('loadData function called');
$("#load_data").append("test");
}
var x = 0;
while (x < 3) {
x = x + 1;
alert('X number is: ' + x);
loadData();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="load_data"></p>
alert()just makes that much more noticeable.