What is a good setTimeout interval for polling in a IE?

≯℡__Kan透↙ 提交于 2019-12-25 03:07:30

问题


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.


回答1:


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



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/3596610/what-is-a-good-settimeout-interval-for-polling-in-a-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!