How to tell when an asynchronous recursive function is complete

I have created a function to extract all links on my website. I do this by creating a recursive function using AJAX. My function looks like the following:

function ListLinksArray (strURL, strDiv)
{
   var strTop = strURL.toLowerCase();
   var strTopDiv = strDiv;
   $.ajax(
   {
      url: strURL,
      success: function(data) 
      {
         $(strDiv + " a", data).each(function() 
         {
            strSrcEncode = $(this).attr('href');
            var strSrc = decodeURIComponent(strSrcEncode);
            if (arrLinks.indexOf (strSrc) < 0)
            {
               arrLinkParent.push (strURL);
               arrLinks.push (strSrc);
               $('#imgList > tbody:last').append('<tr><td>'+strURL + '</td><td>' + strSrc + '</td><td></td></tr>');
               ListLinksArray (strSrc, 'div#content');
            }
      });
      },
      error: function(XMLHttpRequest, textStatus, errorThrown) 
      { 
         var iIsFound = strURL.toLowerCase().indexOf(strTop);
      }
   });
}  
 

Open in new window


How do I know when my function is complete? I need to know this to know when to convert the table to an excel spreadsheet.
WestCoast_BCAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OriNetworksCommented:
The ajax call has beforeSend and complete events you can use to track a variable of what is running. Keep in mind complete doesn't always mean success but you can alter this to how your application behaves. I use iCount to store the iteration currently being run. It is scoped outside of the function so it isn't overwritten by recursive calls. When the value reduces to 0 we know all have completed.

var iCount = 0;
function ListLinksArray (strURL, strDiv)
{
   var strTop = strURL.toLowerCase();
   var strTopDiv = strDiv;
   $.ajax(
   {
      url: strURL,
      success: function(data) 
      {
         $(strDiv + " a", data).each(function() 
         {
            strSrcEncode = $(this).attr('href');
            var strSrc = decodeURIComponent(strSrcEncode);
            if (arrLinks.indexOf (strSrc) < 0)
            {
               arrLinkParent.push (strURL);
               arrLinks.push (strSrc);
               $('#imgList > tbody:last').append('<tr><td>'+strURL + '</td><td>' + strSrc + '</td><td></td></tr>');
               ListLinksArray (strSrc, 'div#content');
            }
      });
      },
      error: function(XMLHttpRequest, textStatus, errorThrown) 
      { 
         var iIsFound = strURL.toLowerCase().indexOf(strTop);
      },
beforeSend: function() {
iCount++;
},
complete: function() {
//do this when complete
iCount--;
if (iCount<=0)
{
alert("everything is complete");
}
}
   });
}  

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WestCoast_BCAuthor Commented:
Thank you, I will give this a try
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.