Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 496155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:39:34+00:00 2026-05-13T05:39:34+00:00

Note: Sorry for the amount of pseudo code below, but I didn’t know how

  • 0

Note: Sorry for the amount of pseudo code below, but I didn’t know how else to show what I’m trying. There is actually a lot more code than that in my solution that manages ajax icon’s and status, but I’ve dumbed it down to show the root of my problem.

Basically, I’m trying to create a process loop on a web page and I’m finding it to be very difficult. What I would like to do is display a list of transactions. At the top of the page, I want to present the user with a ‘Process All’ button which will process each transaction one at a time. While a transaction is being processed on the server, the user should be able to cancel the process loop. This would simply stop the NEXT transaction from being sent; very simple cancel. However, due to the asynchronous nature of ajax calls, I’m struggling quite a bit. 🙁

I have it working great in a synchronous loop. However, the user is not able to click the cancel button using the synchronous approach due to the single threaded nature of it. Here is what I have concocted in an attempt to manage this in an asynchronous loop:

<a href="#" id="processAll">Process All</a>
<a href="#" id="cancelProcess">Cancel</a>
<table>
  <tr><td><a href="#" id="processLink1">Process</a></td></tr>
  <tr><td><a href="#" id="processLink2">Process</a></td></tr>
  <tr><td><a href="#" id="processLink3">Process</a></td></tr>
  <tr><td><a href="#" id="processLink4">Process</a></td></tr>
</table>

Below is what the code looks like that handles the above links:

var currentId = null;
var isWaiting = false; 
var userCancel = false;

$("#processAll").click(function(){

  $(this).hide();
  $("#cancelProcess").show();

  userCancel = false;

  // NOTE: this is where I think I need more logic...?

  processNextTransaction();

});

$("#cancelProcess").click(function(){

  $(this).hide();
  $("#processAll").show();

  userCancel = true;

});

$("a[id ^= processLink]").click(function(){

  var id = $(this).attr("id").replace(/^processLink(/d+)$/, "$1");
  doTransaction(id);

});

Below are the helper methods that I use in the three delegates above:

function processNextTransaction()
{

   var anchorId = $("a[id ^= processLink]:first").attr("id");
   var id = anchorId.replace(/^processLink(/d+)$/, "$1");

   function check() 
   {
     if (isWaiting)
       setTimeout(check, 500);
     else
     {
       if (!userCancel)           
         doTransaction(id);
     }
   }
   check();
}

function doTransaction(id)
{
   isWaiting = true;
   currentId = id;

   $.ajax({
       type: "POST",                
       url: "/Transactions/Process/" + id,
       data: {},
       success: successHandler,
       error: failHandler
   });

}

function successHander(result)
{
   // handle result...
   $("#processLink" + currentId).replaceWith(result);
   isWaiting = false;
}

function failHandler(result)
{
   alert("Error: " + result);
   $("#processLink" + currentId).replaceWith("Error!");
   isWaiting = false;
}

Notice that I only call processNextTransaction() one time, and this is really my main problem. I considered doing it from the successHandler and failHandler, but that defeats the purpose of the setTimeout & isWaiting flag, and also causes other problems when the user clicks on a single ‘Process’ link to process a single transaction. This is my first experience with an ajax looping scenario… Please help! 🙂

What is the suggested approach for managing a list of processes in an asynchronous environment?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:39:34+00:00Added an answer on May 13, 2026 at 5:39 am

    This should work:

    <a href="#" id="processAll">Process All</a>
    <a href="#" id="cancelProcess">Cancel</a>
    <table>
      <tr><td><a href="#" class="processLink" id="pl_1">Process</a></td></tr>
      <tr><td><a href="#" class="processLink" id="pl_2">Process</a></td></tr>
      <tr><td><a href="#" class="processLink" id="pl_3">Process</a></td></tr>
      <tr><td><a href="#" class="processLink" id="pl_4">Process</a></td></tr>
    </table>
    

    And the jQuery magic:

    var cancel = false;
    
    $('#processAll').click(function() {    
        process($('.processLink:first'), true);
        cancel = false;
        return false;
    });
    
    $('.processLink').click(function() {
        process($(this), false);
        return false;
    });
    
    $('#cancelProcess').click(function() {
        cancel = true;
        return false;
    });
    
    function process(el, auto) {
    
        if(!cancel) {
            var id = parseInt(el.attr('id').replace('pl_', '')); // Get numeric ID
    
            $.ajax({
               type: "POST",                
               url: "process.php?id=" + id,
               success: function() {
                   el.html('Processed');                     
                   if(auto) { // If auto is true, process next ID                        
                       if($('#pl_'+(id + 1)).size() > 0) {                             
                           process($('#pl_'+(id + 1)), true);
                       }
                   }
               }
           });
        }
    }
    

    The process function uses the success handler to process the next element, if the original process function was called with the variable automatic set to true.

    The cancel won’t cancel the process if it has already started, obviously.

    You might have/need different kind of methods to traverse your process items, but I guess that’s not your problem (this relies on the ID’s being in numerical ascending order).

    A working example can be found here: http://www.ulmanen.fi/stuff/process.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 270k
  • Answers 270k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As far as logging SSIS goes: We use the logging… May 13, 2026 at 1:30 pm
  • Editorial Team
    Editorial Team added an answer It is useful to mark "unknown" or missing values. For… May 13, 2026 at 1:30 pm
  • Editorial Team
    Editorial Team added an answer This works, but only because your paragraph elements are at… May 13, 2026 at 1:30 pm

Related Questions

OK, I hope I explain this one correctly. I have a struct: typedef struct
We've been using the MVP pattern and Winforms with a fair amount of success.
NOTE: Sorry for the long question! I'm trying to understand some key areas behind
I'm obviously not quite getting the 'end-of-file' concept with C++ as the below program
Sorry for the non-descriptive title but I don't know whether there's a word for

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.