ajax - Javascript requests -
i have 2 checkboxes on selection of each 1 raise ajax request in order response server. need call method once when there atleast 2 seconds gap after last request made. idea? means not want call methods when checkboxes clicked continously less 2 seconds gap. how can cancel request made if time gap between requests in less 2 seconds. note want method fired once after last request not followed other requests 2 seconds.
var timeout; cleartimeout(timeout); timeout = settimeout(function () { // call method }, 2000);
note wan excecute method once last request made.
you don't show code, assuming have function doajax()
ajax request, can ensure isn't called until 2 seconds after last click in 2 second period using settimeout()
function this:
var timerid; document.getelementbyid("yourcheckboxidhere").onclick = function() { cleartimeout(timerid); timerid = settimeout(doajax, 2000); };
note doajax
not have parentheses after when passed parameter settimeout()
function.
if need pass parameters doajax()
function change line settimeout()
to:
timerid = settimeout(function(){ doajax(parameters, go, here); }, 2000);
Comments
Post a Comment