0

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.

Torf
  • 1,154
  • 11
  • 29
  • What happens when you inspect the webpage while this is happening? Does it show multiple requests happening quickly or just the 1? While you are long-polling, you won't be able to load other pages/requests on that same connection. – Rylee Mar 19 '21 at 07:10
  • No. It looks quite normal. If I reload the page or open another page in the site (which has nothing to do with the AJAX request it freezes. But I do have another AJAX on this page. To another script on the same site. Is this not possible? – Torf Mar 19 '21 at 07:31
  • 1
    this is not possible if using file-based sessions (which seems to be the default) since the next script won't run until that session file is available – Rylee Apr 07 '21 at 00:59

0 Answers0