When exactly is an Initialization (for Dynamic, Module, etc.) evaluated?
- To: mathgroup at smc.vnet.net
- Subject: [mg122459] When exactly is an Initialization (for Dynamic, Module, etc.) evaluated?
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Sat, 29 Oct 2011 07:10:28 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
Dear all,
Dynamics/Module/etc. have an Initialization option, which, according to
the documentation, is evaluated the first time the Dynamic cell becomes
visible.
Is this the only instance the Initialization is evaluated? Should it be
the case that in a kernel session, an Initialization will not be
evaluated more than once?
Take this example:
Create a cell using
Dynamic[counter,
Initialization :> (If[! ValueQ[counter], counter = 0, counter++];
Print["init ", counter])]
It will show 0.
Now quit the kernel, and wait a couple of seconds. Move the text input
cursor close to the cell. The kernel will re-launch, and the
Initialization will be evaluated. Now resize the window a bit. The
Initialization will be re-evaluated and the counter will show 1.
Is this the intended behaviour or a bug? Generally, when is an
Initialization evaluated? Can I count on it not being evaluated more
than once in a session?
Suppose that we have a Dynamic cell which actually acts as a counter of
some type of event, and is initialized to 0 in the Initialization
option. Can it happen that it will randomly get re-initialized because
of some UI events?