I have a simple JavaScript code for an AJAX request which worked just fine. For performance reasons I tried to switch to long polling, but every try freezes my server or giving me an Error 429 (too many requests). But somehow I seem to be missing a crucial point.
my ajax call is
$(document).ready(function() {
getData();
});
function getData() {
$.ajax({
type: "GET",
timeout: 60000,
url: "./ajax/fetchData.php",
success: function(response){
if (response != "") {
//do someting
} else {
//do something else
}
getData();
/* -> that was the old Version which worked fine
setTimeout(
getData,
15000
);
*/
}
});
};
and in my fetchData.php I have:
$stmt = $db->prepare("SELECT * FROM questions WHERE display = 1");
$output = getQuestion($stmt);
while($output ['question'] == NULL) {
sleep(10);
$output = getQuestion($stmt);
}
echo $output['question'];
function getQuestion($stmt) {
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row;
}
From the example here that should loop until I get a record and then return this record. But somehow it seems I did not get the point.