I have an array of checkboxes in a complex dynamic-html form. Whenever a user clicks one checkbox, a fairly expensive query is generated and submitted to a remote server.
I want to delay this submit action depending on the users next action. If the user clicks several checkboxes quickly, all of the first clicks should be discarded, only the last one is processed and eventually submitted after 1 second or so.
maybe this is a common problem but I have never worked with timeouts before.
I have an array of checkboxes in a complex dynamic-html form. Whenever a user
Share
the method you are trying to employ is called debouncing.
I usually have a generic debounce function in my javascript:
And then when something needs debouncing, I usually debounce the function,
In your case it might be something like:
where
callbackFunctionis the function that does your “expensive jquery” and1000is the timeout.Applying the above would mean that callbackFunction is called at 1 second timeouts, if it is called at any shorter intervals, the call is ignored.