Im trying to do a basic while loop in javascritp, but I'm getting the wheel icon in os x everytime I launch my script. This code is executed when I press an html button:
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
myFunction();
});
});
var num=1;
function myFunction() {
var c = 0;
// Find a <table> element with id="myTable":
var table = document.getElementById("myTable");
// Create an empty <tr> element and add it to the 1st position of the table:
var row = table.insertRow(-1);
// Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
while(num != 5)
{
$.ajax({
type: 'get',
url: 'controller/script.php',
type: "POST",
data: (
{
name: 'Alfro',
surname: 'McDohl',
num: num
}),
success: function(data)
{
$("p.polla").text(data);
var result = $.parseJSON(data);
// Add some text to the new cells:
cell1.innerHTML = result[0];
cell2.innerHTML = result[1];
num = result[2];
},
error: function()
{
alert('error');
},
});
}
}
And this is the php code of script.php:
<?php
$name = $_POST['name'];
$surName = $_POST['surname'];
$num=$_POST['num'];
$num++;
if ($_POST['num']>=10){
echo json_encode(array($name.$c++, $surName.$c++,'0'));
}
else{
echo json_encode(array($name.$c++, $surName.$c++,$num));
}
?>
The meaning of this is because I want to load data from a database, but it takes too much time to retrieve all the info, so I want to show one result with an ajax call and then continue to retrieve the database to show another result, "one per one" and for this I need to work with a while loop, so I'm playing with this while loop, but I can't get it work.