Group clicks into "more" and "less" buttons to make fewer requests in a row

Asked

Viewed 983 times

1

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

How can I do that?

  • Is it really necessary to request the server only to modify the amount of a product? You cannot get the data for calculation in the html page itself, using javascript?

  • It couldn’t be a simple mai thing, like this: Example in jsfiddle, after all, why request on the server, if you can limit the amount of items in an input of type number?

  • Daniel Omine and Ivan Ferrer, get in the cart and see and test this feature by clicking several times on the increase and decrease number of these sites Mickey.com.br and vestindoamesa.com.br, tells me in Ql of them your experience as a user is better.. It’s bad the way it is, but it has to be whimsical.. i n store the cart data in the cookie, save in the bank, because my system has a part of purchases in abandonment or withdrawal, where the shopkeeper can send an email to the customer warning about the purchase..

  • @Daniel Omine msg d up

  • @Ivan Ferrer msg above

  • @Gregoryiyama, back in the same question, you do not need to record this in the bank, the experience was better on www.mickey.com.br, much faster, although when you give a refresh, it does not keep the amount, however this could be solved through setTimeout() + sessionStorage or localStorage. or session even from PHP. Quitting and quitting can be saved when the session expires.

  • You don’t have to record withdrawal every click... record in the session the items he took... every time. And every minute, saved in the bank the list items, you can do it asynchronously, as you can also do it in other ways like cron, etc... it’s better than in the user click experience.

  • @Gregoryiyama, you know you can monitor the page that is being accessed on realtime When you have a connected user, that’s how chats work. You can do this on the cart screen too, and when the screen closes, you can also know that the user has abandoned. Just another option.

Show 3 more comments

1 answer

0

Look... I’m not very experienced... ugly but functional code. I might find a bug. Thank you who gives some hint too.

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>
      <input type="button" id="aumentaQuantidade" value="+"/>
      <input type="button" id="aumentaQuantidade" value="-"/>
	</body>
</html>

However you could do as they said. But here’s what you questioned.

  • 1

    thank you very much msm for your code! i was going to test it here qnd I received the help of a friend and rolled. but thanks msm for the gallows!! &#xA;se quisr r como ficou segue abaixo:&#xA;&#xA;var cartUpdate; &#xA;&#xA;&#xA;function cartUpdateStartTimer( id ) {&#xA; cartUpdate = setTimeout(function(){ carrinho_atualiza_item(id) }, 1000);&#xA;}&#xA;function cartUpdateStopTimer(){&#xA; clearTimeout(cartUpdate); } Function cart_item_more( id, value_unit ){ cartUpdateStopTimer(); var Qtd = $('#Qtd-item-'+id). val(); Qtd++; Qtd = ( Qtd >= 99 ) ? 99 : Qtd; $('#Qtd-item-'+id). attr('value',Qtd); cartUpdateStartTimer( id ); }

  • 1

    one function starts the count and the other to ai qnd the guy clicks on the more, cancels the count, adds 1 item, and starts the count, if the guy clicks again he returns the function, cancels the count and increments one more, if he to d click the count continues, if given the time it processes the information and updates the page c the function q ta inside the cartUpdateStartTimer won mt in performance and UX !!!

  • I’m glad I could help you. Unfortunately I didn’t have time to comment better or better name the functions. Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.