1

I would like to have the browser wait for any notifications but stop waiting if a link is clicked. That's why I have a PHP script which as a while loop, which ends if something happens and then returns the event.

PHP

require 'required.php';

ignore_user_abort ( false ); // I hoped this does it
set_time_limit ( 60 );

$lock = false; // maybe this helps?

register_shutdown_function ( function () {
    // click the stop button in your browser ...
    $lock = true;
    exit ();
} );

if (isset ( $_SESSION ['user'] ))   {

    while ( ! new_event () && !$lock)   {

        if (connection_status () != CONNECTION_NORMAL) {
            break; // and maybe this works???
        }
        sleep(1);
    }

echo json_encode (array('someparameter','...') );
} else {
echo 'login first';
}

My JQuery AJAX code starts the waiting after load and calls abort() after a link is clicked

var pollReq;
$(document).ready(function() {
        doPoll(); // do the first poll

});

function doPoll() {
   pollReq = $.post("wait.php", {parameter: value}, function(result) {
    // new event, do something
    doPoll();
    // this effectively causes the poll to run again as
    // soon as the response comes back
}, 'json');
}

And if a link is clicked

$('a[href]').not('noajax').click(
        function(e) {
            e.preventDefault();
            if (typeof pollReq !== 'undefined') {
            pollReq.abort(); // stop waiting
            }
   // Do something...
});

I can't find the mistake/cause, maybe someone finds it. Thank you

1 Answer 1

2

After so many tries and searching, I finally found out, that this is all about sessions. Thanks to this post, I changed the PHP, to work like so in the while loop

PHP

while ( ! newMsg ( $chatID, $lastID ) ) {
    // Did the connection fail?
    if (connection_status () != CONNECTION_NORMAL) {
        break;
    }
    session_write_close();
    sleep(1);
    session_start();
}

If you have a better solution, I would be happy to see it.

EDIT: This only works because it allows another script to take the session lock() during that 1 second in between.

AND

If you don't need to write anything to the session: put session_write_close(); before the while loop and leave out session_start();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.