Group Abstract Group Abstract

Message Boards Message Boards

How to force Dynamic Clock in a Button to wait for mouse click to start?

POSTED BY: Jules Manson
Posted 3 years ago

countdown (Clock)
Here is my latest iteration of countdown. As you can see it's wrapped in an anonymous Function which has attributes HoldAll (should not Evaluate until all arguments are provided) in an attempt to force the Clock in countdown to wait on a mouse event. A few of the various Buttons attempted are shown afterwards.

countdown =
Function[{s, n}, Dynamic[s - Clock[{0, s, 1}, s, n]]];

(* demo countdown timer *)

Row[{"almost bare naked countdown \[Rule] ", countdown[10, 1]}]

Button 0: Global

This is the only Button not wrapped in a DynamicModule and in which label x is set to an intermediary variable named timer (this variable is only used here) so that Clock in countdown can be observed to start without a Click event (Evaluate Cells may need to be executed more than once). Immediate Evaluation (before mouse event) is not how a Button is supposed to behave.

Quiet@ClearAll[x];
Row[{"Global label (x) set to intermediate variable (timer) \[Rule] ", 
  Button[Dynamic[x = timer], (timer := countdown[60, 1]) &, 
   ImageSize -> {75, 30}]}]

Button 1: Click

After momentary wait click Button. If less than 60 Clock in countdown did not wait for Click event. Also note that Clock continued to run dynamically despite not displaying updated value in label x before Click event. This even continues to run if Cell containing Button is scrolled out of view. This is not how Dynamic is supposed to work. Although AutoAction -> False is the default setting it is explicitly set for reasons which will become clear in Button 2: (Hover).

DynamicModule[{x}, 
  Row[{"DynamicModule Button with regular Click \[Rule] ", 
    Button[Dynamic[x, (#) &, 
      Initialization :> (x = "wait 5s then Click")], (x := 
        countdown[60, 1]) &, ImageSize -> {150, 30}, 
     AutoAction -> False]}]]

Button 2: Hover

After momentary wait hover Button. If less than 60 Clock in countdown did not wait for Hover event. Again note that this Button 2 (Hover) exhibits all the same dynamics as Button 1 (Click). That is because AutoAction does not supercharge Evaluation. The only thing it does is emulate a Click event with Hover.

DynamicModule[{x}, 
Row[{"DynamicModule Button with Hover \[Rule] ", 
   Button[Dynamic[x, (#) &, 
     Initialization :> (x = "wait 5s then Hover")], (x := 
       countdown[60, 1]) &, ImageSize -> {150, 30}, 
    AutoAction -> True]}]]

Why Stress Click vs. Hover?
I stress the role of option AutoAction because I posted similar questions before at mathematica.stackexchange.com...

https://mathematica.stackexchange.com/questions/253172/please-help-with-dynamic-autosave-button-that-only-halfway-works
https://mathematica.stackexchange.com/questions/253544/my-dynamic-button-evaluates-as-soon-as-its-created-how-do-i-stop-this-behavior
but the few well-meaning good folks who tried to help only provided halfway good but mostly unsatisfactory answers. They were under the mistaken impression that AutoAction would control Clock evaluation or that there was nothing wrong with my code. I believe they didn't wait long enough after Button was rendered to observe the immediate invocation of countdown without a mouse event. It was mostly my bad for not properly setting up demos. That is what I tried to avoid here.

Seems quite easy but there are some mathematical issues with your code. Are you done with it to still under process ?

POSTED BY: Diane Farrow
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard