-1

i have a PHP process who send X mails After each mail send, i add a line on a database for tell that a mail is send.

So, i wan't to create a progress bar for tell to the user that X mails are sended on Y ( Y = total)

I have two jquery function like that :

function mail_send()
{
    var refreshIntervalId;
    $('.loading').css('display','block');
    $.ajax({
        'url': '/an/url/', 
        'data': {
                    someParam: param
                },
        'beforeSend':function()
                    {
                        $('#loadingBg').append('<span id="count"></span>');
                        refreshIntervalId =setInterval(function () {
                            mail_updateProgress(id);
                        }, 500);
                    }

        , 
        'success':  function (data,textStatus)
                    {
                       (....)
                       clearInterval(refreshIntervalId);
                       javascript: console.log(' send finish !!!');
                    }
    });
}

function mail_updateProgress(id) {
    javascript: console.log('updatePG');
    $.ajax({
        'url': '/an/url/', 
            'data': {
                        someParam: param
                    },
        'success':  function (data) {
                        var res = $.parseJSON(data);
                        javascript: console.log('  => data received ' + res.nbSended);
                        $('#count').html(res.nbSended + '/' + res.total);
                    }
    });
}

Is there a queue for the $.ajax ? I'm loggin 3 things : first : When the process is enter on the updateProgress function second : when the updateProgress ajax is successed third : When the mail_send() ajax is successed

the order of the log is :

  • updatePG => 100 times (i've made a sleep in my php code for test it)
  • send finish !!! => 1 times
  • => data received 11 => X times (i've send 11 mails)

So for me it's tell that there is a queue for the ajax call. But how can i perform my progress bar so ?

EDIT : It may be a php configuration problem,

Someone can tell me how allow multi connection from the same processus ?

Mr_DeLeTeD
  • 825
  • 1
  • 6
  • 18
  • I don't exactly understand what you need but MAYBE you can create a text file in which the PHP script writes the progress and an Ajax function to repeatedly check this file and ddisplay the progress? – Shubham May 11 '12 at 11:30
  • This is the same question, how execute an $.ajax() function in the same time than an other ! :s – Mr_DeLeTeD May 11 '12 at 11:34
  • The set interval is ever wrote ! Someone from the #jquery irc tell me to watch if my apache can execute multi-process, i'll check – Mr_DeLeTeD May 11 '12 at 11:48
  • Ricardo Lohmann : the question is "How to perform two ajax request in the same time?" – Mr_DeLeTeD May 11 '12 at 12:27

2 Answers2

1

You're likely running into the issue that a web browser is designed to have no more than 2 concurrent web requests running for any domain. You're trying to fill both those slots with this function. Without knowing more information about the application you're building, specifically, then that's about all I can guess at right now.

A better way to do this, I think, is to look into terminating your client connection for your mail-send request and continuing that process beyond the client termination. This will free up a http request slot for your browser, and should allow your mailProgress ajax function to continue to run as written, without having to worry about queues, and such.

Here's another Stack Overflow question regarding ignoring client termination: Can a PHP script continue running after ending the HTTP request?

and a blog post with more details on how to do it (which is also linked in the above Stack Overflow question): http://waynepan.com/2007/10/11/how-to-use-ignore_user_abort-to-do-process-out-of-band/

Hope that helps.

Community
  • 1
  • 1
Jim Rubenstein
  • 6,836
  • 4
  • 36
  • 54
  • So, i have to "stop" my mail_send() function and let the other run until the value is equal to 100% ? – Mr_DeLeTeD May 11 '12 at 12:50
  • 1
    your mail_send request, instead of being a long-running HTTP request, turns into a request that triggers your script to send e-mail. You have a progress-checking script, so use that to fire your onComplete event for your application. The mail_send request still runs on the Server, the client just isn't directly attached to it anymore. – Jim Rubenstein May 11 '12 at 12:55
  • ok, it's look really good, but what can i use for "forgot" the attachement? Because $.get will be attached to ? – Mr_DeLeTeD May 11 '12 at 13:01
  • I'm not sure I know what you mean – Jim Rubenstein May 11 '12 at 13:19
  • You tell me to stop the $.ajax({}) of the mail_send() method, just, i don't know how do that, all the jquery method i've ever use for call PHP got a "success" event so they are all waiting for HTTP 202 answer – Mr_DeLeTeD May 11 '12 at 13:22
  • 1
    I added the links my my answer that show you how to do it. You terminate the connection on the server side, not the client side – Jim Rubenstein May 11 '12 at 13:26
  • that's my bad, sorry ! I just have to perform a flush() to tell that ? – Mr_DeLeTeD May 11 '12 at 13:33
  • 1
    ``$response = json_encode(array('success' => true)); ignore_user_abort(true); header("Connection: close"); header("Content-Length: " . mb_strlen($response)); echo $response; flush(); //rest of the mailing code`` – Jim Rubenstein May 11 '12 at 13:38
0

I think you're looking for $.done()

Norse
  • 5,674
  • 16
  • 50
  • 86
  • The probleme is not to display the progress bar, the probleme is to display the values of this progress bar (i.e. : 13/124) – Mr_DeLeTeD May 11 '12 at 10:50
  • With my code, i just can display 11/11 cause the $.ajax:success of my mail_updateProgress() function is call after the mail_send() function and not during it ! your ` $('.loading').html('1/2 functions complete! Still working...');` have to be call when ALL is finish and the `$('.loading').html('Done!');` during the process. May it's hard to understand, sorry :s – Mr_DeLeTeD May 11 '12 at 10:53