I would consider using a timer to solve this tricky dilemma.
When focus is lost, start the timer. Then you can cancel the timer if focus is then set upon another "safe" input.
Something like this:
var timeoutID;
$("#TheSafeZone input").focus(function () {
if (timeoutID) {
clearTimeout(timeoutID);
timeoutID = null;
}
});
$("#TheSafeZone input").blur(function () {
releaseTheHounds();
});
function releaseTheHounds() {
timeoutID = setTimeout(function () {
alert("You're all going to die down here");
}, 1);
}
NOTE: I have set the timeout to just 1ms
, this seems to work reliably for me but it may be worth doing a few tests in other browsers (I am using Chrome). I guess it is down to how the JS engine handles events, but I don't know enough about that to confidently say all browsers will work the same