Why doesn't setInterval fire in button Mouse Down event?

Explorer ,
Apr 04, 2019 Apr 04, 2019

Copy link to clipboard

Copied

I'm trying to create a `setInterval` that fires every 5 ms while the mouse is pressed on a button.

Strangely, while other code in the Mouse Down event fires, the setInterval doesn't fire until the event is over. It then fires during the Mouse Exit or Mouse Up event, which is too late.

Why is that? And how can I get the desired behavior?

I looked in the SDK documentation and found a onMouseHit event which sounds more like what I'm looking for. But I don't know how to trigger this event from a form button.

TOPICS
Acrobat SDK and JavaScript

Views

963

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Apr 05, 2019 Apr 05, 2019
You've got a number of issues here. First is that 5ms is a really bad test interval. This will fill up the console window in no time. Probably lock up Acrobat can cause it to crash.  Use something you can watch like 500ms. Next, all of the variables are in a local scope. These need to be document level or global.  For example: this.greeting = ...Next, the MouseUp hide errors with an unreported catch. How do you know this is even working.And Finally, this won't work anyway.  The Acrobat JS event ...

Likes

Translate

Translate
Adobe Community Professional ,
Apr 04, 2019 Apr 04, 2019

Copy link to clipboard

Copied

Post your code.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 05, 2019 Apr 05, 2019

Copy link to clipboard

Copied

Inside the MouseDown event:

function hi() { console.println( "hello world"); };

var greeting = app.setInterval("hi()", 5);

Inside the MouseUp and MouseExit events:

try {

    var timeout = app.setTimeOut("app.clearInterval(greeting); app.clearTimeOut(timeout)", 2000);

} catch(e) {}

Ideally, the setInterval would fire continually during the Mouse Down event and the setTimeOut would fire as soon as the event changes to either MouseUp or MouseExit. In that case, the setTimeOut would be something like 0 or 1, not 2000. I set it to 2 seconds just to demonstrate that the setInterval is indeed firing but only after the MouseDown event.

I also tried firing the setInterval in other mouse events and did not encounter this issue. The setInterval fired during the mouse events as expected. Only during the MouseDown is there an issue.

Can someone please explain why this happens and how to get the desired behavior? Is this a bug, or is there something more to the MouseDown event? Is there a way to create an onMouseHit event instead of a setInterval during MouseDown?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 05, 2019 Apr 05, 2019

Copy link to clipboard

Copied

You've got a number of issues here. First is that 5ms is a really bad test interval. This will fill up the console window in no time. Probably lock up Acrobat can cause it to crash.  Use something you can watch like 500ms.

Next, all of the variables are in a local scope. These need to be document level or global.  For example: this.greeting = ...

Next, the MouseUp hide errors with an unreported catch. How do you know this is even working.

And Finally, this won't work anyway.  The Acrobat JS event structure is somewhat primitive. You know they designed this in the very early days of JS. The system is more or less single threaded. When the user is holding the mouse down the timer events are blocked. This is also true for timer events used in other contexts, such as a popup dialog. You may be able to fool it by creating an event cascade, i.e., use the mouse event to cause another event that triggers the timer, such as changing a value on hidden field. But I don't know if this will work.

You can test this block by add two more buttons to the PDF. In the first button, add code for turning the interval off. In your original button remove the code for turning the interval off, so now you have one button for turning it on and one for turning it off. And one button that does absolutely nothing.

Open the console window so you can watch the messages (make the interval watchable). Click to start the interval, then click and hold the button that does nothing. It will block the messages being printed to the console. Lift up the mouse and the messages will continue.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 14, 2019 Jun 14, 2019

Copy link to clipboard

Copied

Hi, Thom Parker​. Thanks for your reply! I don't remember receiving a notification that someone had answered, so I'm sorry for the late response.

I tried the test that you suggested, and it is as you said. The timer is suspended during mouse down events. In fact, the "do nothing" button isn't even necessary - the timer is suspended as long as the mouse is down, period. I can interrupt the fired events just by pressing the mouse down on a blank area of the page.

I also tried the "event cascade" that you suggested by triggering the setInterval OnFocus, but as you surmised, it doesn't work.SetFocus does not fire until the mouse down event is finished. At this point the onFocus is triggered, then the setInterval is triggered and behaves just as before - pausing every time the mouse is down.

I will have to scrap what I was trying to achieve and find another way. Can you please tell me how I can find out more about Acrobat JS event structures? I'd like to know more about the unreported Mouse Up catch errors you mentioned and if other events behave the same way.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 16, 2019 Jun 16, 2019

Copy link to clipboard

Copied

LATEST

The only way to learn about the events is to test them. The documentation only lists the order of events and the main properties of the event object.  If you look closely you'll find undocumented event properties. Also, there is no information about when events coincide, such as your timer experiment, you just have to test them.

And it gets more complicated. Several other PDF viewers implement some level of Acrobat JS functionality.  Some are quite good such as PDF Tracker and BlueBeam, and for mobile PDF Expert is really good. However, event triggering and execution for all of these viewers stray from the Acrobat implementation. Some quite dramatically. So if the code you write is heavily dependent on how Acrobat implements events, it probably won't work on any other viewer.

About the unreported catch. I was referring to your code.

  1. try
  2.     var timeout = app.setTimeOut("app.clearInterval(greeting); app.clearTimeOut(timeout)", 2000); 
  3. } catch(e) {}

The catch statement does nothing. It catches the exception, which stops it from being propagated, but doesn't do anything, so you don't know if an exception was thrown.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines