Performance issues in onclick javascript handler
I wrote the game in a java script and while it is running it is slow to respond with a few clicks. Below is a very simplified version of the code I am using to handle clicks and still cannot respond to the second click with 2 unless you wait long enough. Is this something I just need to accept, or is there a faster way to be ready for the next click?
By the way, I am attaching this function using AddEvent from the quirksmode transcoding 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 can be seen at http://www.omega-link.com/index.php?content=testgame
+1
a source to share
4 answers
You can try to change the class name instead of removing / adding the div to the DOM (which is what the innerHTML property does).
Sort of:
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;
}
}
}
+3
a source to share