Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

jQuery onfocus double fire problem

Posted on 2013-05-12
4
Medium Priority
?
450 Views
Last Modified: 2013-05-12
Hello,

I am trying to track the amount of times someone goes to a webpage then leaves the tab and then goes back to it but I am having an issue with the jquery double firing.

Here is my code:

<script type="text/javascript">
var f = 1;
$(window).focus(function(){
	f++
	console.log("window clicked on " + f + " times")
});
</script>

Open in new window


The console looks like this (my actions are in italics):
click to foucs
window clicked on 2 times
window clicked on 3 times
click off the tab then click back
window clicked on 4 times
window clicked on 5 times
click off the tab then click back
window clicked on 6 times
window clicked on 7 times


Does anyone know how to stop the double fire?

Thanks!

Nico
0
Comment
Question by:NicoM79
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39160060
Checked in FF and IE10 and it only fires once.
0
 

Author Comment

by:NicoM79
ID: 39160069
Hi GaryC123,

Sorry if I wasn't clear. I don't mind that it starting at two.

To show you what I mean i recorded a quick 18 sec screencast video
http://screencast.com/t/9bkCJ3Ma

This is what I did:

I open up a test html page with the script on it then tab to the next tab a W3 school page and go back to my original test page. Each time I leave the page to go to the next tab and go back it puts two new console.log statements.

Let me know if that makes sense.

Thanks!

Nico
0
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 39160086
Must be a Chrome bug because it works fine in IE and FF, amended your code to fix it.

var f = 1;
var gotFocus = false;  
$(window).focus(function(){

	if(gotFocus){
		return; 
	}
	gotFocus = true;

	f++
	console.log("window clicked on " + f + " times")
});
$(window).blur(function() {
	gotFocus = false;
});

Open in new window

0
 

Author Closing Comment

by:NicoM79
ID: 39160094
Thanks a lot!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

596 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question