1

I have a weak server

When clients repeatedly request ajax service, the server stops working

Frequent demand ajax

My server is weakening

I want to make only one request. Upon completion he will be able to make another request

function checkItemd(item_id){
$("#checkBtn"+item_id).html("Processing..(Wait)").removeClass("btn-success").addClass("btn-primary");
alert("One tool checked at a time - Click OK");
var payload_string = $("#payload_form").serialize();
$.ajax({
    type:"POST",
    url:"ajax-item-check",
    data:payload_string + "&itemId=" + item_id,
    dataType:"json",
    success:function(result){
        if (result.result=="success"){
            if (result.works=="success"){
                var checkBtnMessage = result.response ? result.response : "'Sent to ' Email ";
                $("#checkBtn"+item_id).html(checkBtnMessage).removeClass("btn-primary").addClass("btn-success");
            }else{
                $("#checkBtn"+item_id).html("Error").removeClass("btn-primary").addClass("btn-danger");
                setTimeout('removeRow('+item_id+');',1000);
            }
        }else{
            $("#checkBtn"+item_id).html("Not available to sellers").removeClass("btn-primary").addClass("btn-warning");
        }
    
    }
});
return false;

Abu Adam
  • 13
  • 2

1 Answers1

0

If you want to stop the user making multiple parallel requests, you can just set a flag which causes the function code not to be executable if the request is already in progress.

e.g. look at the requestInProgress flag in this example:

var requestInProgress = false;

function checkItemd(item_id) {
  if (requestInProgress == true) return false;

  $("#checkBtn"+item_id).html("Processing..(Wait)").removeClass("btn-success").addClass("btn-primary");
  alert("One tool checked at a time - Click OK");
  var payload_string = $("#payload_form").serialize();
  requestInProgress = true;

  $.ajax({
    type:"POST",
    url:"ajax-item-check",
    data:payload_string + "&itemId=" + item_id,
    dataType:"json",
    success:function(result){
        requestInProgress = false;

        if (result.result=="success") {
            if (result.works=="success") {
                var checkBtnMessage = result.response ? result.response : "'Sent to ' Email ";
                $("#checkBtn"+item_id).html(checkBtnMessage).removeClass("btn-primary").addClass("btn-success");
            }else{
                $("#checkBtn"+item_id).html("Error").removeClass("btn-primary").addClass("btn-danger");
                setTimeout('removeRow('+item_id+');',1000);
            }
        }else{
            $("#checkBtn"+item_id).html("Not available to sellers").removeClass("btn-primary").addClass("btn-warning");
        }
    
    }
  });
}

N.B. you might want to add an "error" callback so you can set the flag false again in the event of any kind of unexpected problem with the request. Otherwise the user would have to refresh the page before they could make the request again.

ADyson
  • 57,178
  • 14
  • 51
  • 63