Group "plus" and "minus" button clicks to make fewer requests followed

1

In a site I have a "plus" and "minus" button to change the amount of an item in the cart, only the item is updated and clicked. If the client clicks 10 times in a row, 10 requests are made, and the server takes time to execute all processes and then gives timeout. I need to be able to click as many times as I want on the button to increase the quantity, and then, when I stop clicking, after 2 seconds, update the values, making only one request.

How can I do this?

    
asked by anonymous 09.12.2015 / 20:43

1 answer

0

Look ... I'm not very experienced ... ugly but functional code. Maybe find a bug. Thanks for any tips.

var cont = 0;
    
    $("#aumentaQuantidade").click(function() {
        cont = 0;
        setTimeout("contarTempo()", 1000);
    });

    $("#diminuirQuantidade").click(function() {
        cont = 0;
        setTimeout("contarTempo()", 1000);
    });
    
    function contar() {
        cont++;
    }

    function contarTempo() {                
        $('body').append(cont);
        if (cont == 2)
        {
            minhaRequisicaoAjax();
            cont = -1;
        }
        
        if (cont != -1)
        {
            setTimeout("contar()", 1000);
        }
    }
    function minhaRequisicaoAjax() {
        // seu código de requisição ...        
        alert("requisição ajax . . . ");
    }
<!DOCTYPE html>
<html lang="pt-BR">
	<head>
		<meta charset="utf-8"/>
		<title>Página Teste</title>
		<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js"></script></head><body><inputtype="button" id="aumentaQuantidade" value="+"/>
      <input type="button" id="aumentaQuantidade" value="-"/>
	</body>
</html>

However you could do as they said. But that's what you questioned.

    
09.12.2015 / 23:47