views:

340

answers:

3

I have a Yahoo map with lots of markers (~500). The map performs well enough until I close the page, at which point it pauses (in Firefox) and brings up a "Stop running this script?" dialog (in IE7). If given long enough the script does complete its work.

Is there anything I can do to reduce this delay?

This stripped down code exhibits the problem:

<script type="text/javascript">
var map = new YMap(document.getElementById('map'));
map.drawZoomAndCenter("Algeria", 17);

for (var i = 0; i < 500; i += 1) {
    var geoPoint = new YGeoPoint((Math.random()-0.5)*180.0, (Math.random()-0.5)*360.0);
    var marker = new YMarker(geoPoint);
    map.addOverlay(marker);
}
</script>

I'm aware of some memory leaks with the event handlers if you're dynamically adding and removing markers, but these are static (though the problem may be related). Oh, and I know this many markers on a map may not be the best way to convey the data, but that's not the answer I'm looking for ;)

Edit: Following a suggestion below I've tried:

window.onbeforeunload = function() {
    map.removeMarkersAll();
}

and

window.onbeforeunload = function() {
    mapElement = document.getElementById('map');
    mapElement.parentNode.removeChild(mapElement);
}

but neither worked :(

A: 

You could try removing all the markers, or even removing the map from the DOM using the "onbeforeunload" event.

Diodeus
Thanks for the suggestion -- I tried both of these (see question), but with no luck.
David Bick
A: 

Are you sure nothing is tryin to access the map , when you close the window?

I'd do this type of test:

have a wrapper to reach the map itself, and, on unload, have the wrapper block access to map itself.

Joshi Spawnbrood
+1  A: 

Use Javascript profiler and see which function is slow. Then you'll have better idea how to make a workaround or at least how to remove expensive cleanup (and let it leak in IE6).

porneL