Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid hanging when closing a Yahoo map with lots of markers

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 :(

like image 232
David Bick Avatar asked Nov 15 '22 17:11

David Bick


1 Answers

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).

like image 81
Kornel Avatar answered Dec 04 '22 08:12

Kornel