由 alert() 修复的 JavaScript 方法处理 [英] JavaScript method processing fixed by alert()

查看:26
本文介绍了由 alert() 修复的 JavaScript 方法处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用 地图服务 API (http://mapy.cz 特别)用于根据提供的地图数据获取图像作为一些附加绘图的背景.

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.

在浏览器组件截图由 SWT GC 实例制作之前,我试图隐藏地图层的控件,使其尽可能干净.此功能由服务的 JS API 以这种方式提供:

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');
}

通过从控制台调用 hideControls() 函数,在所有桌面浏览器中都可以完美运行.

This works absolutely flawless in all desktop browsers by calling hideControls() function from console.

但是如果该方法被 SWT 浏览器组件调用

But if the method is called by SWT Browser component by

browser.evaluate("hideControls();"); 

它只是在 alert() 函数之前不起作用(正如现在评论的那样)被取消注释.然后显示警报对话框并按应有的方式隐藏控件.

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.

知道为什么 alert() 方法调用会导致该 JavaScript 函数正确工作吗?

Any idea why alert() method call cause correct working of that JavaScript function?

编辑

一些额外的代码上下文

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方法(由地图服务的JS API提供)

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))
};

例如(用于 HUD 控制)

So for instance (for HUD control)

SMap.Layer.HUD.prototype.removeItem = function (a) {
    a.parentNode.removeChild(a)
};

这意味着它直接更改 DOM 树,尽管它不是异步的..

That means it directly change the DOM tree, though it's not asynchronous..

推荐答案

正如 Sean Kinsey 所暗示的,这确实是浏览器组件的问题,需要暂停,直到 JavaScript 线程完成所有必要的步骤.

As Sean Kinsey hinted it's really problem of browser component, that needs to be stalled until JavaScript thread finishes all necessary steps.

我找到了解决方法,因为 SWT GUI 组件必须自己处理它们的事件,但在 JavaScript 完成之前可能会停止浏览器.

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();
}

这是一个丑陋的解决方法,但浏览器做一些琐碎的 DOM 操作需要 20 个滴答"就足够了(我希望如此).

It's ugly workaround, but 20 'ticks' for browser to do some trivial DOM manipulation should be enough (I hope).

这篇关于由 alert() 修复的 JavaScript 方法处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆