views:

47

answers:

1

Hello,

I have a containing div that has multiple divs within which is updated every 25ms using innerHTML (for performance reasons). I have tried using event delegation to capture events but nothing I seem to do captures the click event. I think this may be due to the speed that the contents are getting updated. Any ideas would be very welcome.

My code:

$(document).ready(function () {
var canvas = $('#gCanvas')[0];

$(document.body).delegate('a', 'click', function (e) {
    console.log(e.target);
});

var sprites = [];
for (var i = 0; i < 100; i++) {
    sprites[i] = {x: i+10, y: i+10};
}


var doIt = function () {
    var s = '';

    for(var i = 0; i < sprites.length; i++) {
        var spr = sprites[i];
        spr.x++;
        spr.y++;

        s+= '<a id="s'+i+'" class="s" style="left:'+sprites[i].x+';top:'+sprites[i].y+'"></a>';
    }

    canvas.innerHTML = s;
};
//doIt();
setInterval(doIt, 50);

});

Thanks

Mark

+1  A: 

it easy if you use jquery. there is a .live function, but i am not sure if it quick enough. alternatively you could try to check jquery source to find the .live() code and find the solution

UPDATE

if you are trying to make a game then you can try to use svg for good browsers and vml for bad ones.

Maksim Burnin
The solution that `live` uses is event delegation, which the OP says he's doing.
bobince
SVG lacks the performance you can get compared with innerHTML
Mark Gerrard
@Mark Gerrard: ok then you can try to detect the mouse position when user clicks on the overlay over the game viewport/field and find the nearest div.
Maksim Burnin
I may have come up with a much tidy solution. Let me do some testing.
Mark Gerrard