views:

269

answers:

2

I am trying use jQuery to poll dynamic DOM nodes, created outside of the jQuery object (with Google Maps API methods). I can do this easily by, for example, binding delegate() to a click event. However, I need to poll the DOM, without any additional user actions (user should not have to click), as part of a function that runs onload. Does anyone know of a way to accomplish this?

Edit: I'm using the Maps API to write add a bunch of markers at load. I can do this without any problems, but I need to loop through the HTML the Maps API writes with jQuery and append child nodes. delegate() and live() can this, but the only way I know how to fire delegate() or live() is by binding it to an user event. I'm trying to fire off something like jQuery's delegate with each iteration of my Maps API function, without the user doing anything.

A: 

do the call once:

setTimeout("doGoogleMapApiCall()", 5000);

.. or multiple times:

setInterval("doGoogleMapApiCall()", 5000);

EDIT: I think what you are looking for is a custom event? Have a look here: http://www.reynoldsftw.com/2009/04/custom-events-in-jquery-open-doors-to-complex-behaviors/

harpax
That's kind-of a bad way to do "setTimeout" or "setInterval" - you've already got the function defined, so there's no reason to pass it in as a string. `setTimeout(doGoogleMapApiCall, 5000);` is better.
Pointy
I would recommend assigning setInterval's return value to a variable (e.g., myInterval). Then, later in the page if you need to stop polling, you can call clearInterval(myInterval) to stop polling.
Lobstrosity
My Map API method already fires at load. My issue is I'm trying to loop through the HTML written by the Map API with jQuery, then append child text nodes.
Will Peavy
Thanks harpax. Writing a custom event looks like the solution.
Will Peavy
A: 

Check out the LiveQuery plugin. Here is a simple example of its use:

$('table tr:even').livequery(function(){
    $(this).removeClass("odd");
    $(this).addClass("even");
});

This snippet would automatically apply the "even" class and remove any "odd" class that might be on a table row dynamically as new rows are added to the table.

leek