Adding delay to a $.post jquery request -


i'm sending jquery $.post request on checkbox change in form. want delay $.post 500 ms in case user checks more 1 checkbox rapidly, avoid multiple useless requests.

here's code, i've added settimeout function seems work except $.post function...

var delay = (function(){   var timer = 0;   return function(callback, ms){     cleartimeout (timer);     timer = settimeout(callback, ms);   }; })();  $(document).ready(function() {        $('.checkbox').change(function() {          delay(function(){                                          $.post("/?page_id=4", $("#selectors").serialize(), function(data){                 $('#results').html(data);             });          });      }, 1000 );  }); 

any idea why doesn't work?

live example

this:

$('.checkbox').change(function() {      delay(function(){                                      $.post("/?page_id=4", $("#selectors").serialize(), function(data){             $('#results').html(data);         });      });  }, 1000 ); 

should be:

    $('.checkbox').change(function() {          delay(function(){                 alert('post');                         $.post("/?page_id=4", $("#selectors").serialize(), function(data){                 $('#results').html(data);             });          }, 1000);      }); 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -