views:

190

answers:

5

I have written a game in java script and while it works, it is slow responding to multiple clicks. Below is a very simplified version of the code that I am using to handle clicks and it is still fails to respond to a second click of 2 if you don't wait long enough. Is this something that I need to just accept or is there a faster way to be ready for the next click?

BTW, I attach this function using AddEvent from the quirksmode recoding contest.

var selected = false;
var z = null;
function handleClicks(evt) {
    evt = (evt)?evt:((window.event)?window.event:null);
    if (selected) {
        z.innerHTML = '<div class="rowbox a">a</div>';
        selected = false;
    } else {
        z.innerHTML = '<div class="rowbox selecteda">a</div>';
        selected = true;
    }
}

The live code may be seen at http://www.omega-link.com/index.php?content=testgame

A: 

I believe your problem is the changing of the innerHTML which changes the DOM which is a huge performance problem.

Daniel A. White
Ok, I've actually tried this with both innerHTML and DOM methods. Both seem to respond at the same speed. First click responds with out a bit of delay (the block gets highlighed) and the second click works great if it doesn't occur too soon after the first (I'm guessing at the timming here, but around 1/4 second)In the normal game a user would click on a block to highlight it, then click again to remove the block.
Tom
A: 

I went on your site and clicked as fast as possible. The background switches color very quickly. May I suggest a non code related issue here ?

MickTaiwan
+2  A: 

You could try to only change the classname instead of removing/adding a div to the DOM (which is what the innerHTML property does).

Something like:

var selected = false;
var z = null;

function handleClicks(evt) 
{
    var tmp;

    if(z == null)
       return;

    evt = (evt)?evt:((window.event)?window.event:null);
    tmp = z.firstChild;
    while((tmp != null) && (tmp.tagName != 'DIV'))
        tmp = tmp.firstChild;
    if(tmp != null)
    {
      if (selected) 
      {
        tmp.className = "rowbox a";
        selected = false;
      } else 
      {
        tmp.className = "rowbox selecteda";
        selected = true;
      }
    }
}
Miky Dinescu
While it doesn't address the problem that I was having, you do make an excellent point about not deleting and recreating if I don't have to. +1
Tom
+1  A: 

I think your problem is that the 2nd click is registering as a dblclick event, not as a click event. The change is happening quickly, but the 2nd click is ignored unless you wait. I would suggest changing to either the mousedown or mouseup event.

great_llama
Changing to mouse up seems to have done the trick. Thanks. Of course, now I need to lookup how to remove the handler in a cross platform manner, but that is minor.
Tom
+1  A: 

Yeah you may want to compare the performance of innerHTML against document.createElement() or even:

el.style.display = 'block' // turn off display: none.

Profiling your code may be helpful as you A/B various refactorings:

steamer25
In general a good idea, so the +1. However, I had tried both ways and there was little if any difference.
Tom