add focus and blur events in your code to check when field is focussed and blurred. update boolian variable to set focus or blur state .Check that variable to perform any operation which you want to perform on focus or non focus (blur ) of textField.
$.txt_username.addEventListener('focus', function() {
focussed = true;
});
$.txt_username..addEventListener('blur', function() {
focussed = false;
});
if(focussed){
//do whatever you want when field is focus
}else{
//do whatever you want when field is not focus
}