Wolfram Language

Channel Framework

Multi-User Countdown Clock

Define a channel that displays a countdown clock and a function that drives the countdown.

In[1]:=
Click for copyable input
CreateChannel["Countdown", HandlerFunctions -> Association["MessageReceived" -> ((Countdown`$time = #Message) &)], Initialization :> CreateWindow[ DocumentNotebook[{Dynamic[ ClockGauge[$time, PlotTheme -> "Business"]]}], WindowSize -> All, WindowElements -> {}, ShowCellBracket -> False, Background -> Black, WindowFrameElements -> {"CloseBox"}, Selectable -> False, WindowTitle -> "Countdown", CellContext -> "Countdown`"], Permissions -> Association["All" -> "Read", "Owner" -> {"Write", "Execute"}]]
Out[1]=
In[2]:=
Click for copyable input
countdown[t_] := DynamicModule[{paused = True, lbl = "Pause", ii = t, up = None}, Row[{Button[Dynamic[If[paused, "Start", lbl]], paused = ! paused; up = If[paused, None, UpdateInterval -> 1]], Dynamic[Refresh[ If[ii >= 0 && ! paused, " \[LongRightArrow] " <> TextString[ChannelSend["Countdown", ii--]], If[ii <= 0, up = None; lbl = "Done"]; ""], up, TrackedSymbols :> {paused}]]}]]

Subscribe to the channel.

In[3]:=
Click for copyable input
listener = ChannelListen["Countdown"]
Out[3]=

Set up a countdown for a certain number of seconds and click the button to start.

In[4]:=
Click for copyable input
countdown[20]
Out[4]=

The clock updates while the countdown runs, unless you pause it.

Out[5]=

Other users can subscribe to your countdown clock by referring to your channel in ChannelListen.

In[6]:=
Click for copyable input
$WolframID <> ":Countdown"
Out[6]=
In[7]:=
Click for copyable input
$WolframID <> ":Countdown"; ChannelListen[%]
Out[7]=

Related Examples

de es fr ja ko pt-br ru zh