3

This seems to me to be a very easy question, but I can't seem to figure it out. Instead of updating every 1 second, it auto-updates as quickly as possible.

Clear[t];
t = 1; Dynamic[Refresh[t++, UpdateInterval -> 1, TrackedSymbols :> {t}]]

I've checked the documentation to no avail. Could someone explain this behaviour an propose a fix?

E.O.
  • 1,213
  • 1
  • 12
  • 16
  • From the docs: "UpdateInterval->t specifies that updating should, if possible, be done at least every t seconds." You could use a scheduled task to update a symbol, and then display the symbol with Dynamic. – mfvonh Jul 22 '14 at 15:02
  • @mfvonh I have edited the question to clarify my problem. – E.O. Jul 22 '14 at 15:10
  • @Kuba That seems like a duplicate. Do you not think so? – Mr.Wizard Jul 22 '14 at 17:58
  • @Mr.Wizard I think so but no 100% sure. Sure enought to cast a close-vote though :) – Kuba Jul 22 '14 at 22:03
  • @Mr.Wizard Sometimes stuff is hard to find in the docs but in this case your answer is basically there as an explicit example (under Scope). So I'd mark it a duplicate (of the docs). – Mike Honeychurch Jul 22 '14 at 23:17
  • @Mike I have no problem with this being closed, but I contend that this is not "easily found in the documentation" because the docs (v10) give no explanation for TrackedSymbols -> {} there. I realize it is teaching by example but at the same time sometimes it takes a description for the "aha!" moment. – Mr.Wizard Jul 23 '14 at 03:32
  • @Mr.Wizard agree that the documentation is very poor. It leaves it up to user to figure it out rather than explain what is happening in examples. – Mike Honeychurch Jul 23 '14 at 03:45

2 Answers2

4

When a Symbol is "tracked" it means that when its value changes the Dynamic expression is refreshed, therefore your input updates as fast as possible because every refresh changes the value of t which causes a refresh which... you get the picture.

Compare:

Clear[t];

t = 1; Dynamic[Refresh[t++, UpdateInterval -> 1, TrackedSymbols -> {}]]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
1

Refresh only sets a limit on how long expression can go without being updated. To keep t form being updated more often add a pause.

t = 1; Dynamic[Refresh[Pause[1]; t++, UpdateInterval -> 1, TrackedSymbols :> {t}]]

or do what Mr.Wizard indicated, which is better.

But this sort of thing is best done by higher level constructs such as Clock or Trigger. For example:

status[t_, t1_] := Row[{t, " > ", threshold, " is ", t > t1}]

Dynamic[With[{dt = 1, tmax = 10, threshold = 4}, 
  t = Clock[{0, tmax, dt}, tmax, 1]; status[t, threshold]]]

The above will run for 10 seconds and will update once a second showing whether or not the value of t has passed t = 4.

Trigger gives more control. The following does what the previous example does, but allows the process to be paused and restarted further, the process can be repeated without re-evaluating the code.

With[{dt = 1, tmax = 10, threshold = 4},
  Column[{
    Dynamic @ status[t, threshold],
    Trigger[Dynamic@t, {0, tmax, dt}, dt, 
      AppearanceElements -> {"PlayPauseButton", "ResetButton"}]}]]
m_goldberg
  • 107,779
  • 16
  • 103
  • 257