问题
I have SWT application which uses map service API (http://mapy.cz particularly) for acquiring image as background for some additional drawings based on that provided map data.
Before the browser component screenshot is made by SWT GC instance, I'm trying to hide the controls from map layer, to have it cleanest as possible. This functionality is provided by service's JS API this way:
function hideControls() {
var controls = map.getControls();
for (var i = controls.length; i > 0; i--) {
map.removeControl(controls[i]);
}
//alert('test');
}
This works absolutely flawless in all desktop browsers by calling hideControls()
function from console.
But if the method is called by SWT Browser component by
browser.evaluate("hideControls();");
it just do not work until the alert() function (as commented right now) is uncommented. Then alert dialog is shown and controls hidden as they should be.
Any idea why alert()
method call cause correct working of that JavaScript function?
EDIT
Some additional code context
browser.evaluate("hideControls();");
// ugly ie scroll bar force workaround
// calls JS methods which returns size of map content in browser component
Double mapWidth = (Double) browser.evaluate("return getSizeX();");
Double mapHeight = (Double) browser.evaluate("return getSizeY();");
// creates new canvas
GC gc = new GC(browser);
// do the screenshot
capturedImage = new Image(display, mapWidth.intValue(), mapHeight.intValue());
gc.copyArea(capturedImage, 0, 0);
// cleanout
gc.dispose();
JavaScript methods for map size calculation (provided by JS API for map service)
function getSizeX() {
return map.getSize().x;
}
function getSizeY() {
return map.getSize().y;
}
EDIT2
After some deeper debug the method which removes controls from map layer is
SMap.prototype.removeControl = function (a) {
var b = this.controls.indexOf(a);
- 1 != b && ((a = a.getContainer()) && this.controlLayer.removeItem(a), this.controls.splice(b, 1))
};
So for instance (for HUD control)
SMap.Layer.HUD.prototype.removeItem = function (a) {
a.parentNode.removeChild(a)
};
That means it directly change the DOM tree, though it's not asynchronous..
回答1:
That alert
has a visible effect here points in the direction of you having a race condition, where the blocking alert
call allows the other 'thread' of execution to catch up.
So, you might want to take a look at the order of which your code executes, for instance by using console.log
throughout to trace.
回答2:
As Sean Kinsey hinted it's really problem of browser component, that needs to be stalled until JavaScript thread finishes all necessary steps.
I've found workaround, cause in SWT GUI components have to take care about their event handling themselves, it's though possible to stall the browser until JavaScript finishes.
browser.evaluate("hideControls();");
for(int i = 0; i < 20; i++) {
if(!display.readAndDispatch()); display.sleep();
}
It's ugly workaround, but 20 'ticks' for browser to do some trivial DOM manipulation should be enough (I hope).
来源:https://stackoverflow.com/questions/14631967/javascript-method-processing-fixed-by-alert