Javascript请求

I have two checkboxes on selection of each one will raise a ajax request in order to get response from server. I need to call a method only once when there is atleast 2 seconds gap after the last request is made. Any idea? This means i do not want to call the methods when checkboxes are clicked continously for less than 2 seconds gap. How can i cancel the request made if the time gap between the requests in less than 2 seconds. Note that i want the method to be fired only once after the last request is not followed by other requests for 2 seconds.

var timeout; 
clearTimeout(timeout); 
timeout = setTimeout(function () { // call method }, 2000); 

Note that i wan to excecute the method only once for the last request made.

You don't show any code, but assuming you already have a function doAjax() that does the ajax request, you can ensure it isn't called until two seconds after the last click in any two second period by using the setTimeout() function to do something like this:

var timerID;
document.getElementById("yourCheckboxIdHere").onclick = function() {
    clearTimeout(timerID);
    timerID = setTimeout(doAjax, 2000);
};

Note that doAjax does not have parentheses after it when passed as a parameter to the setTimeout() function.

If you need to pass parameters to your doAjax() function change the line with setTimeout() to:

    timerID = setTimeout(function(){
       doAjax(parameters, go, here);
    }, 2000);