How to handle multiple requests being sent in JavaScript?

1.4k Views Asked by At

Working on a platform, to enable auto-ticketing functionality. For which a REST API request is used for ticket creation. Unfortunately, there are 2 requests popping simultaneously, which results in creating duplicated tickets.

How to handle such case and send only one of these requests?

Tried adding the 2nd request in the response callback of the first, though this does not seem to work.

if (flag == 1){
 logger.debug("Node-down alarm-Request raised - +sitn_id);
 clearTimeout(mouseoverTimer);
 mouseoverTimer  = setTimeout(function(){  
 logger.debug("Inside Call back function - ");
        //function call for ticket creation
 incidentRequest(sitn_id,confUtil.config.mule_url);
}, 10);
            
           

1

There are 1 best solutions below

0
Apps-n-Add-Ons On

You really should show more of the code that makes the request, though it seems as if you are doing some ajax inside your 'incidentRequest', so I will presume that (if that isn't what you are doing, then please, show your code....) - and since you tags say javascript and jquery - well, here goes...

To stop the 'double send' in an AJAX call, it is simple:

function incidentRequest(sitn_id,confUtil.config.mule_url){
    // stop the double by clearing the cache
    $.ajaxSetup({cache: false});
    // continue on with the AJAX call
    // presuming the url you want is confUtil.config.mule_url
    // and the data you want to send is sitn_id
     $.post(confUtil.config.mule_url, 'sitn_id=' + sitn_id, function (data) {
         // do cool stuff
     });
}

Hopefully that will help you get moving. If not, then we will need more code of what is going on around all this.