-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 ?

4
  • 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? Commented May 11, 2012 at 11:30
  • This is the same question, how execute an $.ajax() function in the same time than an other ! :s Commented May 11, 2012 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 Commented May 11, 2012 at 11:48
  • Ricardo Lohmann : the question is "How to perform two ajax request in the same time?" Commented May 11, 2012 at 12:27

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

So, i have to "stop" my mail_send() function and let the other run until the value is equal to 100% ?
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.
ok, it's look really good, but what can i use for "forgot" the attachement? Because $.get will be attached to ?
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
$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
|
0

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

2 Comments

The probleme is not to display the progress bar, the probleme is to display the values of this progress bar (i.e. : 13/124)
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

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.