Function Repository Resource:

TimedPaneSelector

Source Notebook

Create a PaneSelector that changes content on a schedule

Contributed by: Sjoerd Smit

ResourceFunction["TimedPaneSelector"][content,Dynamic[state],rules]

displays a PaneSelector with content that periodically changes state as specified by rules.

ResourceFunction["TimedPaneSelector"][content,{Dynamic[state], Dynamic[t]},rules]

updates t with the time remaining until the next state change.

Details and Options

The rules indicate how the state should move from one value to the next. When state is changed to one of the values that occur as keys of rules, a countdown will be triggered that initiates the change when it expires. During this time, t will be updated continuously to reflect the amount of time remaining.
ResourceFunction["TimedPaneSelector"] takes the following options in addition to all the options that PaneSelector takes:
UpdateInterval0.1how frequently the remaining time should be checked
"ResetTime"<||>time before the state changes (can be state dependent)
"DefaultResetTime"1.reset time to use if the "ResetTime" option is unspecified for a given state
The "ResetTime" option can be an Association or function that will be applied to a state to obtain the time that a state should be active for.
To specify default content that goes into the third argument of PaneSelector, add the rule _ default to the content specification.

Examples

Basic Examples (2) 

Create a PaneSelector that switches between two states periodically:

In[1]:=
state = 1;
ResourceFunction[
 "TimedPaneSelector"][{1 -> "State 1", 2 -> "State 2"}, Dynamic[state], {1 -> 2, 2 -> 1}]
Out[2]=

The Dynamic variable tracks the current state of the PaneSelector:

In[3]:=
Dynamic[state]
Out[3]=

This one will change to the second state and then stop changing:

In[4]:=
DynamicModule[{state = 1},
 ResourceFunction[
  "TimedPaneSelector"][{1 -> "State 1", 2 -> "State 2"}, Dynamic[state], {1 -> 2}]
 ]
Out[4]=

Show how much time is left before a change:

In[5]:=
DynamicModule[{state = 1, t},
 Column[{
   ResourceFunction[
    "TimedPaneSelector"][{1 -> "State 1", 2 -> "State 2"},
    {Dynamic[state], Dynamic[t]},
    {1 -> 2, 2 -> 1}
    ],
   Dynamic[t]
   }]
 ]
Out[5]=

Scope (1) 

Resetting the state externally will reset the timer (if the state is specified to change at all):

In[6]:=
DynamicModule[{state = 1, t},
 Column[{
   SetterBar[Dynamic[state], Range[3]],
   ResourceFunction[
    "TimedPaneSelector"][{1 -> "State 1", 2 -> "State 2", 3 -> "State 3"},
    {Dynamic[state], Dynamic[t]},
    {1 -> 2, 2 -> 1}
    ],
   Dynamic[t]
   }]
 ]
Out[6]=

Options (4) 

UpdateInterval (1) 

Update the remaining time more frequently:

In[7]:=
DynamicModule[{state = 1, t},
 Column[{
   ResourceFunction[
    "TimedPaneSelector"][{1 -> "State 1", 2 -> "State 2"},
    {Dynamic[state], Dynamic[t]},
    {1 -> 2, 2 -> 1},
    UpdateInterval -> 0.1
    ],
   Dynamic[t]
   }]
 ]
Out[7]=

ResetTime (2) 

Specify that state 1 should be active for one second and state 2 should be active for five seconds:

In[8]:=
state = 1;
Column[{
  ResourceFunction[
   "TimedPaneSelector"][{1 -> "State 1", 2 -> "State 2"},
   {Dynamic[state], Dynamic[t]},
   {1 -> 2, 2 -> 1},
   UpdateInterval -> 0.1,
   "ResetTime" -> <|1 -> 1, 2 -> 5|>
   ],
  Dynamic[t]
  }]
Out[9]=

ResetTime accepts Dynamic values:

In[10]:=
DynamicModule[{state = 1, t, t0 = <|1 -> 1, 2 -> 5|>},
 Column[{
   ResourceFunction[
    "TimedPaneSelector"][{1 -> "State 1", 2 -> "State 2"},
    {Dynamic[state], Dynamic[t]},
    {1 -> 2, 2 -> 1},
    UpdateInterval -> 0.1,
    "ResetTime" -> Dynamic[t0]
    ],
   Dynamic[t],
   Slider[Dynamic[t0[1]], {0, 10}, Appearance -> "Labeled"],
   Slider[Dynamic[t0[2]], {0, 10}, Appearance -> "Labeled"]
   }]
 ]
Out[10]=

DefaultResetTime (1) 

If the "ResetTime" option does not resolve to a numerical value, the "DefaultResetTime" will be used:

In[11]:=
DynamicModule[{state = 1, t},
 Column[{
   ResourceFunction[
    "TimedPaneSelector"][{1 -> "State 1", 2 -> "State 2"},
    {Dynamic[state], Dynamic[t]},
    {1 -> 2, 2 -> 1},
    UpdateInterval -> 0.1,
    "ResetTime" -> <|2 -> 5|>,
    "DefaultResetTime" -> 1
    ],
   Dynamic[t]
   }]
 ]
Out[11]=

Publisher

Sjoerd Smit

Version History

  • 1.0.0 – 08 February 2022

Related Resources

License Information