I am using jQuery 1.8.
I have a number of checkboxes that the user can check to get information about a specific product. When the field is checked, the function is called and loads the product information into the div. Currently, the function is triggered immediately after each click. So, if a visitor checks all five mailboxes, ajax call will be made five times.
I want the function to work after a certain period of time when the visitor stops clicking. The function should be triggered only once. The goal of the delay is to limit the number of calls and create a smoother user interface.
Here is my HTML:
<input type='checkbox' class='SomeClass' data=prodid='1'> 1 <input type='checkbox' class='SomeClass' data=prodid='2'> 2 <input type='checkbox' class='SomeClass' data=prodid='3'> 3 <input type='checkbox' class='SomeClass' data=prodid='4'> 4 <input type='checkbox' class='SomeClass' data=prodid='5'> 5 <div id='ProductInfoDiv'></div>
Here is my pseudo JavaScript:
// set vars $Checkbox = $('input.SomeClass'); $ProductInfoDiv = $('div#ProductInfoDiv'); // listen for click $Checkbox.click(getProductInfo); // check which boxes are checked and load product info div getProductInfo = function() { // create list of product id from boxes that are checked var QString = $Checkbox.filter(":checked"); // LOAD THE PRODUCT DIV $ProductInfoDiv.load('SomePage.cfm?'+QString); }
So where can I put a delay? How to ensure that the function works only once?
source share