在IE中进行轮询的setTimeout间隔是多少? [英] What is a good setTimeout interval for polling in a IE?
问题描述
我有一个在浏览器(IE)中运行的ActiveX对象(具有我的源代码). ActiveX对象具有一个UI,该UI引发事件.我想在浏览器中响应这些事件.
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.
我不想从ActiveX对象事件中调用JavaScript函数:因此,我希望JavaScript轮询ActiveX对象的方法(例如,您要报告任何事件吗?")
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();
}
什么是好的轮询间隔?
我猜,尽管我不确定,但是工作量很小:它只是在调用ActiveX对象的方法,该方法要么返回已经缓存的字符串,要么返回null.
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.
我想经常轮询:这样看起来浏览器(实际上是JavaScript)会迅速响应ActiveX对象中的UI事件.
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.
100毫秒太小了吗? 50毫秒怎么样?
Is 100 msec too small? How about 50 msec?
在100毫秒的间隔内,我在浏览器中仅看到1%的CPU利用率:但这只是在我的机器上.一般情况下(运行IE的台式机)如何?
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)?
如果这是本机线程,我不必担心每50毫秒将其唤醒一次,但是我几乎没有在IE中运行JavaScript的经验.
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.
推荐答案
我建议每秒轮询一次.
您真的需要即时反应吗?
I would recommend polling once every second.
Do you really need instantaneous reactions?
此外,您不应将字符串传递给setTimeout
.
相反,您应该像htis这样传递函数本身:
Also, you shouldn't pass a string to setTimeout
.
Instead, you should pass the function itself, like htis:
setTimeout(pollForEvents, 1000);
这篇关于在IE中进行轮询的setTimeout间隔是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!