0

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:

  1. User submits form
  2. In a period of 3 seconds, if the window loses focus do console.log(a);
  3. After 3 seconds, if the window loses focus do nothing.
  4. If the user submits again on the same session, repeat from step 1.
Anders
  • 8,307
  • 9
  • 56
  • 88
jQuerybeast
  • 14,130
  • 38
  • 118
  • 196
  • I'm confused about what you're trying to do. Do you want the blur function to fire if the window is blurred after 3 seconds? – Kyle Trauberman Jun 07 '12 at 22:20
  • When you submit, if in a 3 seconds period the window loses focus, trigger console.log(a) – jQuerybeast Jun 07 '12 at 22:21
  • And after the 3 seconds, you don't want the blur to fire? – Kyle Trauberman Jun 07 '12 at 22:24
  • Yes, unless if the input is submitted again (wait another 3 seconds). The idea is sometimes the form opens in a new tab and sometimes not. The user determines whether to open results in a new tab or not. So i want to identify if its opened or not. The way I am using it right now, even if results open in a new tab or not, when the user changes tab or minimize (loses focus of the window), console.log(a) is triggered. – jQuerybeast Jun 07 '12 at 22:26
  • How `input` can be submitted? – VisioN Jun 07 '12 at 22:29
  • @Jonathan Then the `form` is submitted, not `input`. – VisioN Jun 07 '12 at 22:30

1 Answers1

2

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 will unbind from the blur event after 3 seconds, causing the event to not fire.

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

Alternatively, you could do something like this:

$("#form").on("submit",function(){
    var submitted = true;

    var blurFunc = function() {
        if(submitted) {
            console.log(a);
        } 
    }

    $(window).blur(blurFunc);
    setTimeout(function() { submitted = false; }, 3000);
});
Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
  • I may have misunderstood, but I thought they wanted it the other way around: The `blur` should do nothing if it's within three seconds, otherwise it should do whatever. – Anthony Grist Jun 07 '12 at 22:28
  • I wouldnt want to use bind or unbind. That is why I havent used this method. I do know what setTimeout is but I wasnt sure which would be the best or more efficient way to do this. – jQuerybeast Jun 07 '12 at 22:28
  • @AnthonyGrist see comments on the OP's question. – Kyle Trauberman Jun 07 '12 at 22:29
  • -1 because (a) using a string with `setTimeout` is Bad and (b) you're not using `unbind` well in this case (you're unbinding all unbind handlers, rather than just the one you set above). – lonesomeday Jun 07 '12 at 22:30
  • @lonesomeday thanks for the feedback. How else do you use setTimeout except with a string? Every example I've found uses a string, and this is the way I've done it in the past. Also, good point on the unbind call. – Kyle Trauberman Jun 07 '12 at 22:32
  • @lonesomeday I actually didn't get your point. Why string *here* is Bad? And it seems he unbinds only `blur` event, doesn't he? – VisioN Jun 07 '12 at 22:33
  • @KyleTrauberman `setTimeout(function() { ... }, 3000);` is another option. – VisioN Jun 07 '12 at 22:33
  • @KyleTrauberman [With a function object.](https://developer.mozilla.org/en/DOM/window.setTimeout) – lonesomeday Jun 07 '12 at 22:33
  • @VisioN I unbind all blur events. What if there is another event handler on the window blur, this causes it to be removed as well. – Kyle Trauberman Jun 07 '12 at 22:33
  • @Derek Usually true, but why is it EVIL exactly in this situation? – VisioN Jun 07 '12 at 22:35
  • @KyleTrauberman - Because now it is in GLOBAL, instead of scoped. – Derek 朕會功夫 Jun 07 '12 at 22:36
  • @KyleTrauberman For future reference you can also pass a reference to a declared function, like so: `setTimeout(myFunc, 3000);` where you've declared a function named `myFunc` that implements whatever functionality you want. For this relatively simple case, however, an anonymous function is just as good. – Anthony Grist Jun 07 '12 at 22:36
  • @Jonathan I think this is the best (if not only) way to achieve what you are trying to do. – Kyle Trauberman Jun 07 '12 at 22:37
  • @Derek But what's the problem in unbinding event in GLOBAL? – VisioN Jun 07 '12 at 22:38
  • http://api.jquery.com/unbind/ As of jQuery 1.7, the .on() and .off() methods are preferred to attach and remove event handlers on elements. – jQuerybeast Jun 07 '12 at 22:43
  • @Jonathan I take that back, see my edit for another possible solution. – Kyle Trauberman Jun 07 '12 at 22:45
  • @Jonathan Here's a similar jsfiddle using click instead of submit: http://jsfiddle.net/5ZhXn/1/ – Kyle Trauberman Jun 07 '12 at 22:50
  • Your method doesnt work. No matter how long after, when ever you lose focus of the window, the console triggers – jQuerybeast Jun 07 '12 at 22:50
  • Even your fiddle is not what I am looking for. Editing my question now – jQuerybeast Jun 07 '12 at 22:51
  • @Jonathan I used click for simplicity, and it works for me on chrome. I updated the fiddle as well. http://jsfiddle.net/6nkch/ – Kyle Trauberman Jun 07 '12 at 22:52