Skip to main content
BurtonDev
Inspiring
April 5, 2019
Answered

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

  • April 5, 2019
  • 1 reply
  • 2474 views

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.

This topic has been closed for replies.
Correct answer Thom Parker

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.

1 reply

Thom Parker
Community Expert
Community Expert
April 5, 2019

Post your code.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
BurtonDev
BurtonDevAuthor
Inspiring
April 5, 2019

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?

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
April 5, 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 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 PDFScriptingUse the Acrobat JavaScript Reference early and often