views:

58

answers:

3

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  A: 

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);
SLaks
Can't I poll more often? If not, then why not, or in what circumstances should I not? When the user clicks something in the ActiveX object, I'd like the other stuff on the page to respond to that user input 'apparently instantaneously' (so a small fraction of a second).
ChrisW
For one thing, I believe that anything more frequent will interfere with power-saving mechanisms.
SLaks
"Do you really need instantaneous reactions?" Normally when I write a UI, I design/prefer it to be quick/responsive. Otherwise the user clicks something, nothing happens, and they're like "???".
ChrisW
I can see that you might be be right, theoretically; but my limited Googling hasn't uncovered mention of a direct relationship between the settimer interval and reducing power. My uninformed guess would have been that power-saving is controlled by the average CPU utilisation (and the average utilisation seems to be low-ish, even at 100msec, at least on my machine).
ChrisW
@ChrisW: http://msdn.microsoft.com/en-us/library/ms182230.aspx
SLaks
+1  A: 

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.

James Black
100 msec is effectively instantaneous (less than the human eye-hand reaction time), 200 msec is quick but noticeable, and 1000 msec is entire heartbeat or even longer. Do you foresee any trouble with 100 msec?
ChrisW
@ChrisW - I have used 50ms without any problems, and for some browsers I have used 20ms without any issues, so 100ms should be fine, but, it will depend on what else is going on with the page. For example, a 1 second timer would only need to be about 500ms, for accurate time keeping.
James Black
OK, thank you. My preference would have been an event handler instead of polling; but the .NET security won't let me call from within the object across the ActiveX interface (I suppose it doesn't know that what's running on the other side of the interface is 'safe' JavaScript, and not unmanaged code).
ChrisW
A: 

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.

Zafer