tags:

views:

381

answers:

3

Hello,

I wrote some code that asks for a user to enter a date range and click the next button. This triggers an AJAX call to a file that figures out what dates are included within those days and prints them out onto the screen.

If the user changes the dates and presses "next" again, it does the calculation again. After doing this several times in a row it starts to run really slowly and eventually it will crash the browser all together.

Does anyone know why this is happening and what I can do to fix it?

Thanks very much for your time.

$("#list").ajaxComplete(function(request, settings){
     $('#addNewDays').bind('submit', function() {
            $(this).ajaxSubmit(function() {
                  $('#dateDisplay').load('calculateDays.php?employeeId=' + $('#employeeId').val() + '&fromDay=' + $('#fromDay').val() + '&toDay=' + $('#toDay').val());
            });
            return false; // <-- important!
     });
});
A: 

You can disable the button when the request starts, and then re-enable it when the request is finished.

Jonathan Sampson
A: 
function enableMyButton(){
    $("#myNextButton").attr('disabled','false');
}

$("#myNextButton").click(function(){
    $("#myNextButton").attr('disabled','true');
    // do ajax stuff, set the callback function to enableMyButton
});

Something like that should do you right.

inkedmn
Why will disabling and re-enabling the button fix the slowness issue?
A: 

Why are you re-attaching the submit event to the button every time it is clicked? I assume it is because the button is being replaced with the new content. If so limit the fragment coming back to just be the dates, and nothing else.

Leather
Honestly, I'm relatively new to AJAX so maybe I'm going about calling the calculateDays.php file the wrong way. What I'm saying in this code is when the date calculation form is submitted call this calculateDays.php file to run and spit out the days. Now, maybe I'm just spitting back more information than it can handle.
I have used JQuery quite a bit with ASP.NET MVC and in general you want to make the fragment of HTML returned by the ajax call as small as possible. So if your showing a list of dates and an associated submit button just replace the list, and not the surronding area including the button. This means you'll have less data going over the wire and mean you don't have to rebind the button after the ajax call.
Leather