1

Sorry I couldn't find a better title because of my lack of vocabulary.

I have a php script answering to 2 GET parameters : one to start a long process and one to get the process progression.

Something like :

if(isset($_GET['start_generation'])) exit($dossier->generate());
if(isset($_GET['get_status'])) exit($dossier->getStatus());

A button is sending the first request (AJAX POST) and then start a timer that will check the generation status every 2 seconds using another ajax request.

Currently the first request is not ending before the generation is done, so my function to check the status will go directly from 0 to 100.

How can I close the AJAX request before calling generate() ? Can you tell me if output buffering (ob_flush() & friends) is the way to go ?

Thanks

Edit (my ajax code) :

var lastStatus = false;
var checkDossierStatus = function(){
    $.get('".$URL."&get_status', function(status){
        status = parseInt(status, 10) || 0;
        console.log('status '+status);
        if(status === lastStatus) return false;
        lastStatus = status;

        var target;
        var activeTitleID = 0;

        switch(status){
            default:
            case 0:
                // Show something for status 0...
                break;

            case 1:
                // Show something for status 1... etc...
                break;
        }

    });
};


$('.transmission-start-btn').off('click').on('click', function(e){
    e.preventDefault();
    $.post('".$URL."', {transmission_action: 'start'}, function(d){
        checkDossierStatus();
        startStatusCheck();
    });
});
7
  • add your ajax code Commented Feb 1, 2017 at 14:07
  • Not enuff information to help here. Do you working on linux? Commented Feb 1, 2017 at 14:10
  • Ajax code added. Yes i'm on linux but does it really matter ? Commented Feb 1, 2017 at 14:14
  • You can put an additional checkDossierStatus(); before $.post('".$URL."', {transmission_action: 'start'}, function(d){ Commented Feb 1, 2017 at 14:33
  • The status will always be 0 at this point Commented Feb 1, 2017 at 14:35

2 Answers 2

1

You can abort Or Cancle ajax request like this

$(document).ready(
    var xhr;

    var fn = function(){
        if(xhr && xhr.readyState != 4){
            xhr.abort();
        }
        xhr = $.ajax({
            url: 'ajax/progress.ftl',
            success: function(data) {
                //do something
            }
        });
    };

    var interval = setInterval(fn, 500);
);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but i'd rather do that from PHP
0

The best solution to me was to use output buffering with a Connection: Close header and session_write_close().

session_write_close();  # Prevent session locking so AJAX status update request will not hang.
ob_end_clean();
ob_start();
header('Content-Type: text/event-stream');
header('Connection: Close'); # Close the ajax request.
echo 'HEY'; # This will be displayed
$size = ob_get_length();
header('Content-Length: '.$size);
ob_end_flush();
flush();

LongProcess(); # Here a can finally start a long background process

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.