3
$\begingroup$

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?

$\endgroup$
8
  • $\begingroup$ 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. $\endgroup$
    – mfvonh
    Jul 22 '14 at 15:02
  • $\begingroup$ @mfvonh I have edited the question to clarify my problem. $\endgroup$
    – E.O.
    Jul 22 '14 at 15:10
  • $\begingroup$ closely related $\endgroup$
    – Kuba
    Jul 22 '14 at 15:46
  • $\begingroup$ @Kuba That seems like a duplicate. Do you not think so? $\endgroup$
    – Mr.Wizard
    Jul 22 '14 at 17:58
  • $\begingroup$ @Mr.Wizard I think so but no 100% sure. Sure enought to cast a close-vote though :) $\endgroup$
    – Kuba
    Jul 22 '14 at 22:03
4
$\begingroup$

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 -> {}]]
$\endgroup$
1
$\begingroup$

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"}]}]]
$\endgroup$

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