I have 5 checkboxes on a hypothetical page, and if any one of them is checked, I want to perform a postback. However, I would like to delay the postback a bit so that if the user checks a box, it waits a second or so to make sure the user doesn't want to check more boxes before doing the round trip to the server. So, if you checked all five in rapid succession, you'd be able to check all five before the postback is sent out.
Has anyone done this, seen this done, know how it might be done, or have a good reason not to do it at all?
You didn't say you were using jQuery, but I will assume for the sake of discussion and clarity that it is available. Something along the lines of this should work for you:
var timeout = null;
// Set up code to respond to when the checkbox is checked/unchecked
$('input[type="checkbox"]').change(function() {
// If this is the second check box in a row, clear previous timeout
if (timeout != null)
clearTimeout(timeout);
// Set a timeout that will fire in 5 seconds
timeout = setTimeout(function() {
// Post back the form
__doPostBack();
}, 5000);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With