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

  • SEARCH
  • Home
  • 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 6360243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:37:27+00:00 2026-05-24T23:37:27+00:00

I have a checkout I’m playing with which has some up and down arrows

  • 0

I have a checkout I’m playing with which has some up and down arrows next to the quantity input of each item.

I have some repeated code and it doesn’t seem that efficient. Hopefully you can help.

Each item looks something like this (please excuse my non-standard-conforming item_id attribute for now)

<div class="qty_ctrl_wrap">
  <input class="bag_qty" type="text" value="4" item_id="532" name="product_quantity">
  <div class="arrows">
    <div class="more"></div>
    <div class="less"></div>
  </div>
</div>

Here’s the full jquery function:

$('#checkout .arrows div').live('click',function(){

    var item_id = $(this).parent().prev('input').attr('item_id');
    var current_qty = $(this).parent().prev('input').val();

    if($(this).attr('class') == 'more') {

        var new_qty = parseInt(current_qty) + 1;

    } else {

        var new_qty = parseInt(current_qty) - 1;

    }

    // change the input on screen for visual reasons...
    $(this).parent().prev('input').val(new_qty);


    // submit the changes
        $.ajax({
        type: 'POST',
        url: '/backend/product_purchase_items.php',
        data: 'qty='+new_qty+'&item_id='+item_id,
        cache: false,
        success: function(data) {
          $('#product_purchase_items').html(data);
        }
        });     

});

As you can see, I’m repeating the following thing 3 times:

$(this).parent().prev('input')

I’ve been trying to work out how to put that in a variable so I don’t have to repeat that code.

But in addition to that, the way this works is that if you hit the arrow 3 times, it does 3 seperate ajax posts. Ideally there would be a slight pause before sending the post.

How does one make a pause of say 300 milliseconds to see if there are further clicks of the arrows before submitting the post?

  • 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-24T23:37:28+00:00Added an answer on May 24, 2026 at 11:37 pm

    You can use setTimeout to debounce the event:

    $('#checkout .arrows div').live('click',function(){
        var $this = $(this),
            data = $this.data(),
            $item = $(this).parent().prev('input');
    
    
        if(data['debounce_timer']) {
            clearTimeout(data['debounce_timer']);
        }
    
        $item.val(function(i, value) {
            return +value + $this.hasClass('more') ? 1 : -1;
        });
    
        // Maybe you have to store a reference to the quantity (value) here. 
        // Otherwise you might get strange results when the arrow was just 
        // clicked again when the callback is executed.
    
        var timer = setTimeout(function() {
            $.ajax({
                type: 'POST',
                url: '/backend/product_purchase_items.php',
                data: {qty: $item.val(), item_id: $item.attr('item_id')}
                cache: false,
                success: function(data) {
                  $('#product_purchase_items').html(data);
                }
            });    
        }, 150); // <-- try different times
    
        data['debounce_timer'] = timer;
    });
    

    Things that changed/should be considered:

    • Caching $(this).parent().prev('input') in a variable to not traverse the DOM every time to find the same element again.

    • Passing a function to .val() [docs] and update the value “in one go”.

    • Using unary + to convert a string to a number, instead of parseInt. If you use parseInt, you have to pass the radix as second parameter (in your case 10), to prevent JS from interpreting numbers with leading zeroes as octal numbers.

    • Using .hasClass() [docs] instead of comparing the class attribute. If the element has more than one class, the comparison will fail.

    • Setting an object as value of the data option instead of a string. This ensures that the data is properly encoded.

    • You really should use HTML5 data attributes instead of custom attributes (item_id). Even if the browser does not support HTML5 (yet), this is the safer way. With jQuery, you can even access them via the .data() method.

    • Using setTimeout [MDN] to delay the Ajax call and storing a reference to the timer ID in .data() [docs]. When the handler is rapidly executed, a previous timer will be cancelled with clearTimeout [MDN], to prevent too many Ajax requests.

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

Sidebar

Related Questions

There are some products for which I would like to have a special checkout
I have a python script which does a partial checkout. At some point I
Hi have a repository checkout (SVN) which has been heavily modified by a team
I have a ListView on my checkout page with an ItemTemplate which build up
I have a branch called experiment. git checkout master echo 'some changes' > a.txt
i would like to add some Products after the Checkout I have an observer
i have imported(checkout) some read-only repository on the Internet $ svn co http://some.repo/at/somesite read-only
I have made a checkout some time ago from my Subversion Repo to my
I have some important checkout pages that are served via an iframe within a
While reset and checkout have different usages most of the time, I can't see

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.