What is a good setTimeout interval for polling in a IE?
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.
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);
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.
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.
精彩评论