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

Deproblemify
  • 3,340
  • 5
  • 26
  • 41

1 Answers1

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();

Community
  • 1
  • 1
Deproblemify
  • 3,340
  • 5
  • 26
  • 41