5

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

Dylan Corriveau
  • 2,561
  • 4
  • 29
  • 36
David Bick
  • 779
  • 5
  • 11
  • If you cause a break with a debugger (either Firebug or MS Script Debugger) during this pause / delay, what code is executing? – J5. Oct 21 '08 at 03:58

3 Answers3

1

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

Kornel
  • 97,764
  • 37
  • 219
  • 309
0

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
  • 525
  • 6
  • 13
0

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

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176