# What is the point of Refresh if Dynamic has an UpdateInterval option?

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

In[1]:= Dynamic[DateString[],UpdateInterval->1]
Out[1]= Sat 10 Nov 2012 01:36:34
In[2]:= Dynamic[Refresh[DateString[],UpdateInterval->1]]
Out[2]= Sat 10 Nov 2012 01:36:34

-

Let me know if this is not enough, but I'll just quote the docs for now

From the Advanced Dynamic Functionality tutorial:

### Nesting Refresh

In the "Refresh" section examples, Refresh is always the outermost function inside Dynamic. You might almost wonder why its options are not simply options to Dynamic. But in fact it is often important to place Refresh as deeply in the expression as possible, especially if it specifies a time-based updating interval.

Consider this example.

DynamicModule[{showclock = True}, {Checkbox[Dynamic[showclock]],
Dynamic[If[showclock, Refresh[DateList[], UpdateInterval -> 0.05],
"No clock"]]}]


When the checkbox is checked, Refresh is causing frequent updating of the clock, and CPU time is being consumed to keep things up-to-date. When the checkbox is unchecked, however, the Refresh expression is no longer reached by evaluation, the output remains static, and no CPU time is consumed. If Refresh were wrapped around the whole expression inside Dynamic, CPU time would be consumed constantly, even if the clock were not being displayed. The words "No clock" would be constantly refreshed, pointlessly. (This refreshing is not visible; there is no flicker of the screen, but CPU time is being consumed nevertheless.)

-
So what's the difference and why just not use Dynamic instead of Refresh? –  Kuba Nov 6 '14 at 13:27
@Kuba They are VERY different. Refresh evaluates to nothing, it's just a marker that means "if you get to evaluate this snippet when updating the dynamic, then remember these are the update settings I want". Dynamic is something that doesn't evaluate at all, and it means "if this is going to be displayed in the front end, evaluate the contents before showing trying to reevaluate every time the user is looking at it and the result would change" –  Rojo Nov 6 '14 at 16:24
But where's the difference if in the example above you replace Refresh with Dynamic? I know Refresh alone has no point but why to nest Refresh if you just may nest Dynamics? God, sometimes I think I should switch to cycling. –  Kuba Nov 6 '14 at 16:51
@Kuba, its a case where you won't see a visual difference but its just a special case. It's not just that Refresh alone has no point. Its that Refresh doesn't affect calculations, its more like a tag, while Dynamic isn't evaluated. For example, if you changed Refresh[DateList[] to 100 Refresh[DateList[], then it would be different to 100 Dynamic[DateList[]. The Dynamic has to make sense as a visual piece of thing on the screen. –  Rojo Nov 6 '14 at 17:36
I'm back! If you put Print["check"]; showclock in If you can see that whole Dynamic is evaluated, so UpdateInterval setting from refresh is passed to outer Dynamic. IMO this example sucks, using nested Dynamic would be way better habit to learn because the If statement would be only checked when showclock is changed, not each .05 second –  Kuba Mar 30 at 11:19

I agree with Rojo's comments under his answer and I'd upvote them but the example from tutorials given in his answer is more confusing than educational for me (as expressed in comments). "For me", not "in my opinion" so maybe one can learn more from this part of tutorial.

Nevertheless, I want to show you examples were using Refresh really matters.

1. Refresh gives not head to expr which may be very useful (see comments under accepted answer)

 Dynamic[{DateString[], 2 Refresh[2, UpdateInterval -> 1]}]

2. Refresh matters when you have a Dynamic which isn't standard Dynamic that is going to be converted to DynamicBox, I know two exceptions:

• First is usage Dynamic inside user interface elements like Sliders. This was pointed out by Fred Simons here.

So only the first example gives expected result of updating controller:

Slider[Dynamic[Refresh[RandomReal[], UpdateInterval -> 1]]]
Slider[Dynamic[RandomReal[], UpdateInterval -> 1]]

• Second is (FrontEnd|Notebook|Cell)DynamicExpression. If you try to do this without Refresh, with Dynamics only, you will probably fail:

DynamicModule[{x = 0},
Dynamic[{DateString[], x}]
,
Initialization :> (  SetOptions[EvaluationCell[],
CellDynamicExpression :> Refresh[x++,
UpdateInterval -> 1, TrackedSymbols :> {}]
]
)]

-

Refresh also has a TrackeSymbols option. Consider the case where several dynamic variables are declared in the first argument to DynamicModule but you only want updating to occur on a proper subset of those variables. An example is given at the bottom of the answer I gave to this question.

-