# Dynamic and Refresh

If I am not mislead by my memory, in Mathematica 6 a function Refresh was introduced, having the options UpdateInterval and TrackedSymbols, to be used inside Dynamic, that did not have these options. Anyway, nowadays both Dynamic and Refresh have these options, and already many years I never use Refresh, with the feeling that it is a relict from long ago.

In (What is the point of Refresh if Dynamic has an UpdateInterval option?), more or less the same was remarked by @Tom Wellington:

I haven' t found an example where I can' t get rid of Refresh[].

In the answer to that question, an example from the documentation was given. But in that example, we can replace Refresh with Dynamic with the same effect, so for me the question remains open.

Now I happened to find the following behaviour:

Column[{Dynamic[RandomReal[],UpdateInterval->0.5],
Slider[Dynamic[RandomReal[],UpdateInterval->0.5]],
Slider[Dynamic[Refresh[RandomReal[],UpdateInterval->0.5]]]}]

The first Dynamic updates normally. With the same expression in a slider, it does not update at all, but when we wrap it in Refresh, it updates.

Another example:

Column[{Slider[Dynamic[x]],
Slider[Dynamic[1-x, TrackedSymbols:>{}]],
Slider[Dynamic[1-x, TrackedSymbols:>{},UpdateInterval->0.5]],
Slider[Dynamic[Refresh[1-x, TrackedSymbols:>{},UpdateInterval->0.5]]]}]

When we move the first slider, the second and the third slider are immediately updated, while the second slider should not update at all and the third slider only two times per second. The last slider, with the first argument wrapped in Refresh, behaves as expected.

It looks a little bit that we can use the options TrackedSymbols and UpdateInterval in Dynamic only when this Dynamic is a 'stand-alone' expression. As soon as it is part of a controller, we have to use Refresh if we want to use these options. I have not seen this documentated. Am I right?

• There is a difference in the cell expressions that might be significant. My LinkSnooper is broken since our license renewal a couple of days ago, so I can only speculate. (The plain Dynamic is a DynamicBox; the slider is a SliderBox with code, no DynamicBox-es.) – Michael E2 Nov 6 '14 at 13:16
• @MichaelE2 Looks like Slider with Refresh inside behaves as standalone Dynamic[1 - x, TrackedSymbols :> {}, UpdateInterval -> .5]. I mean service request to update object, then call from FE on pre.link and return from kernel. It even stops when you minimize window. – Kuba Nov 6 '14 at 13:35
• So it seems this should be the answer to linked topic :) – Kuba Nov 6 '14 at 13:37
• @Michael. That is a very interesting remark, and it could well be the explanation. However, in all cases the contents of the Sliderbox is a Dynamic expression as in the kernel command, with the kernel variable x replaced with the frontend variable \$CellContextx. So it might be that the options for Dynamic do not work for frontend variables or maybe the frontend function Dynamic does not have these options. Anyway, I feel this could/should be documentated. – Fred Simons Nov 6 '14 at 13:52

It seems to me now that my conjecture was too simple. Here is an example that shows that we might need Refresh inside a Dynamic that is visualized on the screen in a DynamicBox. The crucial point is that this DynamicBox is displayed in a DynamicModuleBox, which has an effect on the updating procedure.

Consider the following example:

Panel[Column[{
Dynamic[x],
Button["press",x=0;Pause[2];x=RandomInteger[{1,9}], Method->"Queued"]
}]]

When the button is pressed, during two seconds the value 0 is displayed and then the new value of x turns up. The option Method->"Queued" is essential for this behaviour.

Now I want to localize the variable x in a DynamicModule.

DynamicModule[{x},
Panel[Column[{
Dynamic[x],
Button["press",x=0;Pause[2];x=RandomInteger[{1,9}], Method->"Queued"]
}]]
]

The output shows the name of the variable that the kernel will use for the variable x in the displayed DynamicModule. Before pressing the key, evaluate the next command, with 00 replaced with the value shown.

Dynamic[FEx$$00] Then press the key. Before the new value is displayed, the Dynamic output shows the value 0, but the DynamicModule does not. The reason is that when the key is pressed, the DynamicModule sends the button action to the kernel for evaluation, waits for the completion of this action and then adapts the display. The evaluation setting Method->"Queued" is used only for the auxilary kernel variable FE`x$$00.

Is it possible to force the DynamicModule to update the inner Dynamic[x] during the kernel evaluation, as with Dynamic not wrapped in DynamicModule?

The answer is yes and the solution is given by Refresh:

DynamicModule[{x},
Panel[Column[{
Dynamic[Refresh[x, TrackedSymbols:>{x}]],
Button["press",x=0;Pause[2];x=RandomInteger[{1,9}], Method->"Queued"]
}]]
]