I am trying to identify if the window loses focus after 3 seconds of submit.
Currently I have this:
$("input").on("submit",function(){
$(window).blur(function(){
console.log(a);
})
});
But this, no matter when you press submit, if you click outside the window or minimize the window the console.log triggers a.
This is what I am trying to achieve:
- User submits form
- In a period of 3 seconds, if the window loses focus do console.log(a);
- After 3 seconds, if the window loses focus do nothing.
- If the user submits again on the same session, repeat from step 1.
Try this:
The
setTimeoutcall will unbind from the blur event after 3 seconds, causing the event to not fire.For more info on
setTimeoutsee here: http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/Alternatively, you could do something like this: