Jump to content

Start and stop ajax Onfocus bug help


Nomax5

Recommended Posts

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

[code]
<?
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>

[/code]

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

it works great and is really small in kb's

cheers


Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.