JS中如何判断一个对象是否就绪?

发布于 2024-09-29 22:26:08 字数 124 浏览 3 评论 0原文

我的网络应用程序包含一个 ActiveX 控件。但是,当我运行该应用程序时,我间歇性地收到错误“预期对象”错误。当我调用其属性/方法时,有时控件似乎尚未准备好。有没有一种方法可以使用 JS 检测对象是否准备好?

多谢。

My web app includes an ActiveX control. However, when I run the app, I got error "object expected" error intermittently. It seems sometimes the control is not ready when I call its properties/methods. Is there a way that I can detect whether an object is ready using JS?

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丑疤怪 2024-10-06 22:26:08

如果它不是您自己的应用程序,请查看是否可以识别一些无害的属性或方法,然后围绕调用设计一个包装方法,用 try catch 测试它是否可以访问该对象,如果可以,则调用链中的下一个方法(可能使用一个包含参数的委托,如果没有准备好,请使用 setTimeout 在 100 毫秒内再次调用包装器。

您可能需要包含一个重试计数器,以在几次尝试后进行救援,以便在对象损坏时不会出现无限循环。

示例:

function TryCallObject(delegate, maxtries, timebetweencalls, failCallback, retrycount)
{
    if(typeof retrycount == "undefined")
        retrycount = 0;
    if(typeof failCallback == "undefined")
        failCallback null;
    try {
        //code to do something harmless to detect if objects is ready
        delegate(); //If we get here, the object is alive
    } catch(ex) {
        if(retrycount >= maxtries)
        {
             if(failCallback != null)
                  failCallback();
             return;
        }
        setTimeout(function () {
              TryCallObject(delegate, maxtries, timebetweencalls, failCallback, retryCount + 1);
            }, timebetweencalls);
    }
}

它的名字是这样的

TryCallObject(function() { /* your code here */ }, 5, 100);

TryCallObject(function() { /* your code here */ }, 5, 100, function() {alert("Failed to access ActiveX");});

If its not you own app, see if you can identify some harmless property or method and then design a wrapper method around the call that tests with try catch if it can access the object, and if yes, call next method in chain (maybe using a delegate to include arguments, and if not ready, use setTimeout to call the wrapper again in say 100 ms.

You might want to include a retry counter to bailout after a few tries so that it's not an infinite loop if the object is broken.

Example:

function TryCallObject(delegate, maxtries, timebetweencalls, failCallback, retrycount)
{
    if(typeof retrycount == "undefined")
        retrycount = 0;
    if(typeof failCallback == "undefined")
        failCallback null;
    try {
        //code to do something harmless to detect if objects is ready
        delegate(); //If we get here, the object is alive
    } catch(ex) {
        if(retrycount >= maxtries)
        {
             if(failCallback != null)
                  failCallback();
             return;
        }
        setTimeout(function () {
              TryCallObject(delegate, maxtries, timebetweencalls, failCallback, retryCount + 1);
            }, timebetweencalls);
    }
}

And its called like this

TryCallObject(function() { /* your code here */ }, 5, 100);

or

TryCallObject(function() { /* your code here */ }, 5, 100, function() {alert("Failed to access ActiveX");});
萌面超妹 2024-10-06 22:26:08

如果是您自己的应用程序,请包含一个就绪状态事件

http: //msdn.microsoft.com/en-us/library/aa751970%28VS.85%29.aspx

If it is your own app, include a readystate event

http://msdn.microsoft.com/en-us/library/aa751970%28VS.85%29.aspx

陪你搞怪i 2024-10-06 22:26:08

我们在 FireBreath (http://firebreath.org) 中执行此操作的方法是向 javascript 触发事件;它通过在

中提供函数名称来实现此目的。标签,获取对浏览器窗口 IDispatch 指针的引用,并对 param 标签中指定的函数执行 PROPERTYGET。

然后,当插件准备就绪时,我们调用该方法。这样做的优点是在所有浏览器中的工作方式几乎相同,因为 FireBreath 插件既可以作为 ActiveX 控件也可以作为 NPAPI 插件工作。

The way we do this in FireBreath (http://firebreath.org) is to fire an event to javascript; it does this by providing a function name in a <param> tag, get a reference to the browser window IDispatch pointer, and do a PROPERTYGET for the function named in the param tag.

We then call that method when the plugin is ready to go. This has the advantage of working pretty much the same way in all browsers, since FireBreath plugins work both as ActiveX controls and NPAPI Plugins.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文