Jump to content


Photo

Start and stop ajax Onfocus bug help


  • Please log in to reply
5 replies to this topic

#1 Nomax5

Nomax5
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationYork England

Posted 24 September 2006 - 11:26 AM

Hi

I have a large number of pages with ajax based chat on and I want to stop it communicating when the window looses focus using javascript
I’ve almost got it working but there is a bug.

It stops communicating when the window looses focus but when the window gets focus again it speeds up communicating, doubles it I think. I would be grateful for any help .

This is the controlling javascript

<?
	sajax_show_javascript();
?>
	
	var check_n = 0;
	var old_sha1 = "";
	var new_sha1 = "";
	<!-- Variable to check if window has focus -->
	var hasFocus = 1;
	
	window.onfocus = function() {
			hasFocus = 1;
			refresh();
	}

	window.onblur = function() {
		hasFocus = 0;
	}
	
	
	function refresh_cb(new_data) {
		new_sha1 = hex_sha1(new_data);
		if(old_sha1 != new_sha1) {
			document.getElementById("wall").innerHTML = new_data;
			old_sha1 = new_sha1;
		}
		document.getElementById("status").innerHTML = "Refreshed: #" + check_n;	
		check_n++;
		setTimeout("refresh()", <?php echo $cfg['refreshrate'] ; ?>); 
	}
	
	function refresh() {
		if(hasFocus == 1) {
			x_refresh(refresh_cb);
		}
	}
	
	function add_cb() {
		// we don't care..
	}

	function add() {
		var line;
		var handle;
		handle = document.getElementById("handle").value;
		line = document.getElementById("line").value;
		x_add_line("[" + handle + "] %spantag%" + line, add_cb);
		document.getElementById("line").value = "";
		// document.getElementById("line").focus();
	}
	// ]]>
	</script>


I’ve been using some code called chatwall from http://www.laan.org/...emos/chatwall/  For quite some time.

it works great and is really small in kb's

cheers




#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 25 September 2006 - 04:04 AM

So this isn't your code?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 Nomax5

Nomax5
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationYork England

Posted 27 September 2006 - 12:12 AM

no it's from a guy called Joop Laan and it in turn uses sajax toolkit from modernmethod.com he's gone gone over to using ruby rails but I like this old version because it's really small and simple. but as I said I'm trying to get it to stop communicating when the window looses focus.



#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 27 September 2006 - 06:56 PM

I can't really help here -- there are functions that I don't see, and I don't have the time to figure out how they might work.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 Nomax5

Nomax5
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationYork England

Posted 28 September 2006 - 10:44 AM

I have the functions they're quites small I didn't post them because
I don't think its what the functions are doing its when they're called

when using IE it works fine as far as I can tell  I open 2 windows side by side and as I move focus between them I can see the refresh counter stop and start

But with firefox the counter doubles up like running twice as fast

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 28 September 2006 - 03:02 PM

Clearly, there's a difference in the way the browsers handle focus... that's about all I can say.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users