Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6230321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:43:35+00:00 2026-05-24T09:43:35+00:00

I have a long polling request on my page. The script on the server

  • 0

I have a long polling request on my page. The script on the server side is set to timeout after 20 seconds.

So, when the long polling is “idling”, and the user presses another button, the sending of that new request is delayed until the previous script times out.

I can’t see anything wrong with the code on jQuery side. Why is the onclick-event delayed?

function poll()
{
$.ajax({
    url: "/xhr/poll/1",
    data: {
        user_id: app.user.id
    },
    type: "POST",
    dataType: "JSON",
    success: pollComplete,
    error: function(response) {
        console.log(response);
    }
});
}

function pollComplete()
{
    poll();
}

function joinRoom(user_id)
{
$.ajax({
    url: "/xhr/room/join",
    dataType: "JSON",
    type: "POST",
    data: {
        user_id: app.user.id,
        room_id: room.id
    }
});
}

<button id="join" onclick="javascript:joinRoom(2);">Join</button>

############ PHP Controller on /xhr/poll

$time = time();
while ((time() - $time) < 20)
{
    $updates = $db->getNewStuff();

    foreach ($updates->getResult() as $update)
        $response[] = $update->getResponse();

    if (!empty($response))
        return $response;
    else
        usleep(1 * 1000000);

    return 'no-updates';
}

Could the “usleep” be the problem?

XHR Screenshot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:43:36+00:00Added an answer on May 24, 2026 at 9:43 am

    If you use sessions in the AJAX handling functions, you may run in to an issue where the server is maintaining session data on disk. If so, the data may be locked by the first request, so each subsequent request ends up waiting for the session data file to be available before it proceeds. In effect, this makes asynchronous calls block one another, you end up with linear responses to the requests in chronological order – synchronous. (here’s a reference article)

    A PHP-specific solution is to use session_write_close (docs) to close out the session as soon as you don’t need it any more. This allows other subsequent requests to proceed because the session data will be “unlocked”. Other server-side languages manage sessions in different ways, but this is usually something you can manage or control through some mechanism.

    Managing sessions can have some pitfalls. If you call session_write_close (or otherwise end a session) right before you return a response, then you aren’t going to do yourself any favors because the session would have been unlocked as soon as the response was sent. Thus, it needs to be called as early as possible.In smaller projects, this isn’t so bad because you often have a php script that just handles the request and dumps a response, but if you have a larger framework and your request handler is only a part of it, you’ll have to explore a more top-level solution to non-blocking session usage so your subcomponents are not closing a session that the framework expects is still open.

    One route is to go with database session. There are pros and cons to this solution which are beyond the scope of this answer – check Google for exhaustive discussion for your particular server-side language. Another route is to use a function that opens a session, adds a variable, then closes it. You risk race conditions with this solution, but here’s a rough outline using PHP as an example:

    function get_session_var($key, $default=null) {
        if (strlen($key) < 1)
            return null;
        if (!isset($_SESSION) || !is_array($_SESSION)) {
            session_start();
            session_write_close();
        }
        if (array_key_exists($key, $_SESSION))
            return $_SESSION[$key];
        return $default;
    }
    function set_session_var($key, $value=null) {
        if (strlen($key) < 1)
            return false;
        if ($value === null && array_key_exists($key, $_SESSION)) {
            session_start();
            unset($_SESSION[$key]);
        } elseif ($value != null) {
            session_start();
            $_SESSION[$key] = $value;
        } else {
            return false;
        }
        session_write_close();
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have script, that does long polling on server to check if user
I have a Runnable running inside a ThreadPoolExecutor long polling on an http request
I have a simple comet chat. JavaScript send ajax request with long polling. When
I have long since forgotten the password for the root user on one of
I have long tables generated by datagrid control that go beyond the page width.
I have long recognized that any set of whitespace in an HTML file will
I have long HTTP request ( generating large Excel file - about 60K records
I'm trying to implement long polling using Netty and jQuery. I have it working
I have a web page that I need to check the server for an
I have an application which uses an open JQuery Ajax connection to do long-polling/comet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.