Ryan, you appear to have two choices :
Leave the event handler as it is, and trigger the keyup event to re-perform ajax on the element's current value
$("#element").trigger('keyup');
This can be performed at any point in your code. There are no scope issues as all code has access to the DOM.
Modify the event handler such that it saves the most recent ajax promise in an outer scope
var lastKeyupPromise;
...
$("#element").on('keyup', function() {
lastKeyupPromise = checkTag($(this).val()).done(checkTagDone);
});
Then, elsewhere in your code :
if(lastKeyupPromise) { // safety is required as `lastKeyupPromise` may still be undefined
lastKeyupPromise.done(checkTagDone);
}
This can be performed at any point in your code providing lastKeyupPromise
and checkTagDone
are in scope.
The advantages of the second aproach are :
- No need to perform another ajax request.
- You are not limited to executing
checkTagDone
. You can choose to execute whatever is appropriate at that point in your code.
For example :
if(lastKeyupPromise) {
lastKeyupPromise.done(someOtherHandler);
}