1
$\begingroup$

I stripped a Dynamic down to its bare essentials. Dynamic doesn't execute any of the functions asked for in second argument. Once the countdown completes a cycle (recurring Clock) it should at a minimum do (Ding[]; NotebookSave[];) however ideally I would prefer it did all of this (Ding[]; NotebookSave[]; x = "saved"; Pause[3]). Please help me solve this.

countdown[s_ : 10, n_ : 3] = Dynamic[s - Clock[{0, s, 1}, s, n]];

Ding[v_ : 3/5] := 
  Quiet@EmitSound[
    Sound[{SoundNote["G#6", .3, "Marimba", SoundVolume -> v]}]];

DynamicModule[
 {s = 6, n = 3, t, x},
 t = countdown[s, n];
 Dynamic[x,
  If[t == 0, (Ding[]; NotebookSave[]; x = "saved"; Pause[3]),
    #] &, Initialization :> (x = t), TrackedSymbols :> {x, t}]
 ]
$\endgroup$
1
  • 1
    $\begingroup$ (1) The second argument of Dynamic is called only "during interactive changing or editing." There is no interactive changing or editing here. (2) t displays as an integer, but its head is Dynamic. So t == 0 is never True and never False. Use Setting[t] == 0 instead. But it never gets executed here because of point (1). $\endgroup$
    – Michael E2
    Sep 26 at 14:03
1
$\begingroup$

Maybe the following? I didn't understand all the extra variables, so I stripped down the code more. In a complicated case, you might need Dynamic@Refresh instead of just Dynamic; see What is the point of Refresh if Dynamic has an UpdateInterval option? and related Q&A on Refresh. You'd probably have to put the output in a docked cell, since the FE updates Dynamic objects only if they're "on-screen."

DynamicModule[{t, saved = False},
 t = Dynamic@Clock[{0, 6, 1}];
 Dynamic[
  If[! saved && Setting@t <= 1,
   Ding[];
   (*NotebookSave[];*)
   saved = True,
   saved = False]
  ; t
  , TrackedSymbols :> {t}
  , UpdateInterval -> 1]
 ]
$\endgroup$
5
  • $\begingroup$ thank you this worked. funny that you mention DockedCell as this Dynamic is embedded in an AttachCell but callled fron a Button in a DockedCell. reason for no Refresh is that t depends on countdown which depends on Clock. Dynamic automatically refreshes any expression tied to a Clock using Clock's own UpdateInterval. $\endgroup$ Sep 26 at 18:54
  • 1
    $\begingroup$ @JulesManson You're welcome. The reason Refresh might be or might not be needed is different. (For instance, UpdateInterval -> 1 is needed, or seems to be: Dynamic does not automatically refresh without it in my test.) Refresh[expr,...] allows you to control whether expr is updated without updating the whole code. In complicated user interfaces, this can save time and make the UI more responsive. Whether it's needed or helpful depends on the overall design, and I didn't know how simplified your mini-example is. $\endgroup$
    – Michael E2
    Sep 26 at 18:57
  • 1
    $\begingroup$ I have not used AttachCell. If it moves far enough off screen, the dynamic updating might not occur. $\endgroup$
    – Michael E2
    Sep 26 at 19:04
  • $\begingroup$ Fantastic argument for including a redundant Refresh when Dynamic[expr... depends on a Clock. I have found AttachCell to be a wonderful alternative to DockedCell when placing AttachCell with respect to EvaluationNotebook rather than another Cell so that it never scrolls out of view. $\endgroup$ Sep 26 at 19:21
  • 1
    $\begingroup$ @JulesManson Sounds nice. I just tried it and it made Mathematica crash. :( $\endgroup$
    – Michael E2
    Sep 26 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.