Jump to content

Perform next loop after the finish of Ajax request


Go to solution Solved by 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.

Edited by Xaotique
  • Solution

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);
    });
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.