I have this code:
$('#page-refresh').click( function() {
$.ajax({
url: "/page1.php",
cache: false,
dataType: "html",
success: function(data) {
$('#pagelist').html(data);
}
});
return false;
});
In this code is it possible that on the ajax success function it disables the #page-refresh click for 5 seconds then re-enable it? Basically if a person clicks the button and this action happens I dont want them to click and run this action again for another 5 seconds. I looked at delay() to unbind the click for this then bind it again but once it unbinded it never allowed me to click the button anymore.
If “#page-refresh” is really a button (a
buttonorinput type="button"element), you can use itsdisabledproperty and then set a timeout to restore it:If it’s not really a button, you can simulate the
disabledproperty. I’ll do it with a class here so you can show the disabled state for the user via CSS:Note that in the first case, I’m keeping a reference to the DOM element (
var refreshButton = this;), but in the second case I’m keeping a reference to a jQuery wrapper around it (var $refreshButton = $(this);). That’s just because jQuery makes it easy to test/add/remove class names. In both cases, that reference is released once the closures in your event handler are released (in the above, that’s five seconds after the ajax call completes).You said specifically you wanted to disable it after the ajax call is complete, but as Marcus points out below, you probably want to disable it when starting the ajax call. Just move the disabling bit up a bit, and add an
errorhandler for the case wheresuccessdoesn’t get called (errorhandlers are usually a good idea in any case):Real button:
Simulated via ‘disabled’ class: