Hi Viv.
While I don't have a direct answer to your question, but I have been burned in the past by not handling events to only run my code once until the code is complete so I like your processingEvent flag.
I did some testing and found this condition "if (window.DeviceOrientationEvent)" will sometimes lie. I have three different tablets running android 4.2.2 and in this case window.DeviceOrientationEvent condition was true but the property values returned null. I confirmed that this is a known problem by doing some googleing. I tested my phone running 4.4 and an onlder ipad it worked fine but as you said the event fires very fast.
>I've been using the above with the following code (Typescript):
var processingEvent:boolean = false;
> if (window.DeviceOrientationEvent) {
> window.addEventListener('deviceorientation', (data): void=> {
> if (Math.abs(lastAlpha - data.alpha) < 2) { return; }
> if (processingEvent) {
> return;
> };
> processingEvent = true;
>
> lastAlpha = data.alpha;
> if (data.absolute === true && data.alpha !== null) {
> this.deviceOrientation = Math.round(360 - data.alpha);
> console.log(this.deviceOrientation.toString());
> this.updateArrows();
> }
> processingEvent = false;
> }, false);
> } else {
> alert('DeviceOrienation not supported');
> }
I noticed the event listener could be called again before the routine has finished (the event is raised extremely frequently) so added the 'processing' flag to prevent re-entrancy. It works fine - but it also appeared to work without the check so I'm wondering whether there is any really benefit in it.
>
>Any opinions ?
>
>Side Rant: I've been testing this with different browsers. Firefox gives by far the best results but Chrome, which one would expect to work well on Android devices, is annoyingly flacky....