Jump to content

Perform next loop after the finish of Ajax request


lilmer

Recommended Posts

Is there any solution on how to process the next loop after the success request on the first ajax request on the first call?

E.g.

$('.email').each(function(){
   
    $.ajax({
       Processs!!!
    });
  
});

or make a wait for the next loop after an ajax call? is this possible?

 

I don't want to use

async:false

because it makes chrome browser to freeze.

Any advice?

$.ajax({
    // Some stuff ..
}).done(function()
{
    // Call Next
});

You can use .done() for successful, .fail() for unsuccessful, or .always() to always trigger after it runs.  You could use .done() and .fail() together I believe to do something depending on which occurs.

What I mean is it will perform the next loop after an ajax process succeeded. . but I got it already.

		$('#test').click(function(){
				var newArray =new Array('sample','code','for','testing');
				var all = newArray.length;			
				var i = 0;
				
				
					
				ajaxProcess(i);
				
			
					   function ajaxProcess(i){
							if(i < all){
									$.ajax({
									 type: "POST",
									url: 'ajax.php',
									data: {'test': newArray[i]['cars']},
									dataType: 'html',
									success:function(data){
									i++
										alert(data);
										$('div#output').html(data);
										ajaxProcess(i);
									}
									});
									
							}
						}
			
			
		});

Yes, you would use them basically the same way.

 

 

function myFunc(i)
{
    $.ajax(
    {
        // Something ..
    }).done(function()
    {
        // Success - Call Next
        myFunc(i + 1);
    }).fail(function()
    {
        // Failed - Retry?
        myFunc(i);
    });
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.