0

I have a form which submits an AJAX request to one of my controllers which uploads a file using PHP's curl. I want to show the user the status of that (PHP) upload, so I store the PHP upload progress in a session variable. Meanwhile, the submission of the form also starts a setInterval() which makes a different AJAX request to controller which checks the session variable. My problem is that the second AJAX call seems to only fire once (instead of throughout the upload process) and so instead of progressively updating the progress, it just returns 100 at the end. What am I doing wrong?

Here's my code:

(note: I'm using the jQuery form plugin to assist with the file upload. It also adds some additional callbacks)

<script>
var check_progress = function() {
    $.ajax(
        {
            url : '/media/upload_progress',
            success : function(data) {
                console.log(data);
                },
            async : false

        }
    );    

var options = {
    beforeSend : function() {
        $("#MediaSubmitForm").hide();
        $("#MediaSubmitForm").after('<img class="hula-hippo" src="/img/hippo-hula.gif" />');

        t = setInterval( check_progress, 500 );

    },
    success : function(data){
        $(".hula-hippo").hide();
        $("#MediaSubmitForm").after("<h3>Upload complete!</h3><p>Do <strong>you</strong> want to <a href='#'>create a project</a> of your own?</p>");
        window.clearInterval(t);
        console.log(data);
    }    

};
$("#MediaSubmitForm").ajaxForm(options);
</script>
emersonthis
  • 32,822
  • 59
  • 210
  • 375
  • 1
    It might be an issue with there being a limit on the number of simultaneous connections you can have. BTW, is there a reason you're setting the async flag to false? – Nathan Breit Jul 26 '13 at 21:45
  • Is the number of connections you referenced for php or from the browser? – emersonthis Jul 27 '13 at 14:20
  • The HTTP spec says browsers should limit simultaneous connections to a server to 2. There could also be a limit on your server depending on how it's configured. – Nathan Breit Jul 27 '13 at 23:26

2 Answers2

0

Use setTimeout();. setInterval() executes the code after the time specified and setTimeout() executes the code every time it reaches the specific time.

These question explain well of their difference :)

And a search of this on SO will solve your problem :)

Community
  • 1
  • 1
Mohammad Areeb Siddiqui
  • 9,795
  • 14
  • 71
  • 113
0

It sounds like this is a PHP locking issue. See the first comment in the answer to this question:

jQuery: Making simultaneous ajax requests, is it possible?

Community
  • 1
  • 1
Nathan Breit
  • 1,661
  • 13
  • 33
  • I think you're right. I read that question and tried adding session_write_close() in a few places, but it's still not quite working. Do you have a sense of where specifically I should add it? – emersonthis Jul 27 '13 at 08:30