Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do delayed postbacks?

Tags:

asp.net

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?

like image 663
quillbreaker Avatar asked Apr 13 '12 22:04

quillbreaker


1 Answers

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);
});
like image 153
Kirk Woll Avatar answered Sep 26 '22 00:09

Kirk Woll