Jump to content


This topic is now archived and is closed to further replies.


Start and stop ajax Onfocus bug help

Recommended Posts


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


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;

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;
setTimeout("refresh()", <?php echo $cfg['refreshrate'] ; ?>);

function refresh() {
if(hasFocus == 1) {

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();
// ]]>


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


Share this post

Link to post
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.

Share this post

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

Share this post

Link to post
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

Share this post

Link to post
Share on other sites
Clearly, there's a difference in the way the browsers handle focus... that's about all I can say.

Share this post

Link to post
Share on other sites


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.