javascript - On submit, after X seconds, if window blur -


i trying identify if window loses focus after 3 seconds of submit.

currently have this:

$("input").on("submit",function(){ $(window).blur(function(){   console.log(a); }) }); 

but this, no matter when press submit, if click outside window or minimize window console.log triggers a.

this trying achieve:

  1. user submits form
  2. in period of 3 seconds, if window loses focus console.log(a);
  3. after 3 seconds, if window loses focus do nothing.
  4. if user submits again on same session, repeat step 1.

try this:

$("#form").on("submit",function(){     var blurfunc = function() {         console.log(a);     }      $(window).blur(blurfunc);     settimeout(function() { $(window).unbind('blur', blurfunc); }, 3000); }); 

the settimeout call unbind blur event after 3 seconds, causing event not fire.

for more info on settimeout see here: http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/

alternatively, this:

$("#form").on("submit",function(){     var submitted = true;      var blurfunc = function() {         if(submitted) {             console.log(a);         }      }      $(window).blur(blurfunc);     settimeout(function() { submitted = false; }, 3000); }); 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -