在 IE 中轮询的 setTimeout 间隔是多少比较合适?

发布于 2024-09-16 14:30:41 字数 981 浏览 5 评论 0原文

我有一个在浏览器 (IE) 中运行的 ActiveX 对象(我有源代码)。 ActiveX 对象有一个 UI,可以引发事件。我想在浏览器中响应这些事件。

我不想从 ActiveX 对象事件调用 JavaScript 函数:因此,我希望 JavaScript 轮询 ActiveX 对象的方法(也就是说,“您有任何事件要报告吗?”)。

我将使用这样的代码来做到这一点:

function findActiveXObject() {
    return document.getElementById('MyActiveXObject');
}
function startPolling() {
    setTimeout('pollForEvents()', 100);
}
function pollForEvents() {
    var activeXObject = findActiveXObject();
    var eventMsg = activeXObject.PollForEvent();
    if (eventMsg != null)
    {
        //do something with the event
        alert(eventMsg);
    }
    //poll again soon
    startPolling();
}

什么是好的轮询间隔?

我想,虽然我不确定,工作量很小:它只是调用 ActiveX 对象的方法,该方法要么返回已缓存的字符串,要么返回 null。

我想经常轮询:这样浏览器(实际上是 JavaScript)就可以迅速响应 ActiveX 对象中的 UI 事件。

100 毫秒太小了吗? 50 毫秒怎么样?

在 100 毫秒的间隔下,我在浏览器中仅看到 1% 的 CPU 利用率:但这只是在我的计算机上。一般情况下(运行 IE 的台式机)怎么样?

如果这是一个本机线程,我不会担心每 50 毫秒唤醒它,但我在 IE 中运行 JavaScript 的经验很少。

I have an ActiveX object (who source code I have) running in a browser (IE). The ActiveX object has a UI, which raises events. I want to respond to those events in the browser.

I don't want to invoke JavaScript functions from the ActiveX object events: and therefore, instead, I want the JavaScript to poll a method of the ActiveX object (to say, "do you have any events to report?").

I'll do that with code like this:

function findActiveXObject() {
    return document.getElementById('MyActiveXObject');
}
function startPolling() {
    setTimeout('pollForEvents()', 100);
}
function pollForEvents() {
    var activeXObject = findActiveXObject();
    var eventMsg = activeXObject.PollForEvent();
    if (eventMsg != null)
    {
        //do something with the event
        alert(eventMsg);
    }
    //poll again soon
    startPolling();
}

What's a good polling interval?

I guess, though I'm not sure, that the amount of work is small: it's just calling a method of an ActiveX object, which either returns an already cached string or returns null.

I'd like to poll frequently: so that it looks like the browser (actually the JavaScript) responds promptly to UI events in the ActiveX object.

Is 100 msec too small? How about 50 msec?

With a 100 msec interval I see only a 1% CPU utilization in the browser: but that's just on my machine. What about in general (desktop mchines running IE)?

If this were a native thread I wouldn't worry about waking it up every 50 msec, but I have little experience with running JavaScript in IE.

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

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

发布评论

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

评论(3

日裸衫吸 2024-09-23 14:30:41

我建议每秒轮询一次。
您真的需要即时反应吗?

另外,您不应将字符串传递给 setTimeout
相反,您应该传递函数本身,例如 htis:

setTimeout(pollForEvents, 1000);

I would recommend polling once every second.
Do you really need instantaneous reactions?

Also, you shouldn't pass a string to setTimeout.
Instead, you should pass the function itself, like htis:

setTimeout(pollForEvents, 1000);
季末如歌 2024-09-23 14:30:41

从 1 秒开始,然后看看你的反应如何。

如果您需要更快,请减少超时时间,但是,您可能会发现,由于操作系统和时间片的原因,低于 20-50 毫秒您将不会获得任何改进,因此线程可以获得足够的时间。

我怀疑如果你没有做太多事情,你会看到很多CPU利用率,因为,如果它需要1毫秒来完成操作,而且它可能会更快,那么你大部分时间都会花在睡觉上。

但是,这实际上取决于用户体验,这是主观的。一个人可以接受的事情对其他人来说可能看起来很慢。

因此,找到您认为合适的值,然后请朋友尝试一下,看看他们对响应的看法。如果没有任何好处,仅仅因为你可以就加快速度是没有意义的。

Start with 1 second, then, see how your response is.

if you need it faster, decrease the timeout period, but, you may find that below 20-50ms you won't get any improvement, due to the OS, and timeslicing, so threads can get adequate time.

I doubt you will see much cpu utilization, if you are not doing much, as, if it takes 1ms for it to do the operation, and it could be faster, then you spend most of your time sleeping, for this.

But, it really comes down to the user experience, and that is subjective. What may be acceptable to one person may seem slow to someone else.

So, find what you think is an appropriate value, then ask friends to try it and see what they think of the response. No point going faster just because you can, if there is no benefit.

走野 2024-09-23 14:30:41

这取决于
- 您希望 ActiveX 对象响应的速度有多快。
- 使 CPU 保持繁忙的其他因素(Flash 动画、其他轮询功能)

由于其他因素,间隔值不反映实际值。因此,在您的机器中,较低的值似乎是正确的,但您无法确定其他客户端的确定性。我建议您尽可能增加间隔。如果一秒钟就足够了,那就没问题了。

It depends on
- how fast you want the ActiveX object to respond.
- other factors that keep the CPU busy (flash animations, other polled functions)

Interval values don't reflect the actual values because of other factors. So, in your machine lower values can seem right but you can't be sure of the certainty at ohther clients. I recommend you to increase the interval as much as you can. If one second is adequate, that's fine.

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