Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <code>setTimeout</code> to <em>debounce</em> the event:</p> <pre><code>$('#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); // &lt;-- try different times data['debounce_timer'] = timer; }); </code></pre> <p><strong>Things that changed/should be considered:</strong></p> <ul> <li><p>Caching <code>$(this).parent().prev('input')</code> in a variable to not traverse the DOM every time to find the <em>same</em> element <em>again</em>.</p></li> <li><p>Passing a function to <a href="http://api.jquery.com/val/" rel="nofollow noreferrer"><code>.val()</code> <em><sup>[docs]</sup></em></a> and update the value "in one go".</p></li> <li><p>Using unary <code>+</code> to convert a string to a number, instead of <code>parseInt</code>. If you use <code>parseInt</code>, you <em>have to</em> pass the radix as second parameter (in your case <code>10</code>), to prevent JS from <a href="https://stackoverflow.com/questions/850341/workarounds-for-javascript-parseint-octal-bug">interpreting numbers with leading zeroes as octal numbers</a>.</p></li> <li><p>Using <a href="http://api.jquery.com/hasClass/" rel="nofollow noreferrer"><code>.hasClass()</code> <em><sup>[docs]</sup></em></a> instead of comparing the <code>class</code> attribute. If the element has more than one class, the comparison will fail.</p></li> <li><p>Setting an <em>object</em> as value of the <code>data</code> option instead of a string. This ensures that the data is properly encoded.</p></li> <li><p>You really should use <a href="http://ejohn.org/blog/html-5-data-attributes/" rel="nofollow noreferrer"><em>HTML5 data attributes</em></a> instead of custom attributes (<code>item_id</code>). Even if the browser does not support HTML5 (yet), this is the safer way. With jQuery, you can even access them via the <code>.data()</code> method.</p></li> <li><p>Using <a href="https://developer.mozilla.org/en/window.setTimeout" rel="nofollow noreferrer"><code>setTimeout</code> <em><sup>[MDN]</sup></em></a> to delay the Ajax call and storing a reference to the timer ID in <a href="http://api.jquery.com/data/" rel="nofollow noreferrer"><code>.data()</code> <em><sup>[docs]</sup></em></a>. When the handler is rapidly executed, a previous timer will be cancelled with <a href="https://developer.mozilla.org/en/window.clearTimeout" rel="nofollow noreferrer"><code>clearTimeout</code> <em><sup>[MDN]</sup></em></a>, to prevent too many Ajax requests.</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload