Message Boards Message Boards

0
|
758 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Repeating slide show with periodic updates?

Posted 3 months ago

I'm stuck on a part of a project that I thought would be easy. I want to cycle through displaying three different panes. After two entire cycles (panes a1, a2, a3, a1, a2, a3), I want it to change the random content and then display the new panes (a1, a2, a3, a1, a2, a3, b1, b2, b3, b1, b2, b3, c1...).

The code that follows is a toy version that works but does not refresh the random content every two cycles. I have tried inserting various configurations of Clock and ScheduledTask. So far all my efforts have resulted in either no updates to the random content (a1, a2, a3, a1, a2, a3, a1, a2, a3, a1, a2, a3...) or continuous updates (a1, b1, c1, d1...). I have also tried searching StackExchange and this Community and asked the LLM for assistance, all without success.

Any help would be appreciated.

POSTED BY: Mark Greenberg
2 Replies
Posted 3 months ago

Method 1:

DynamicModule[{stack, newstack, slide, done = True, last = 0, t},
 newstack[s_, k_] /; s == 1 && last != 1 := (If[done,
    With[{wd = RandomWord[3],
      no = RandomInteger[100, 3],
      co = RandomColor[3]},
     stack = 
      Table[Style[Row[{wd[[i]], no[[i]], co[[i]]}, Spacer[6]], 
        36], {i, 3}]
     ];
    done = False,
    done = True];
   last = s;
   stack[[k]]);
 newstack[s_, i_] := (last = s; stack[[i]]);
 newstack[1, 1];
 slide = Dynamic[
   t = Ceiling[Clock[3, 6]]];
 Framed[
  PaneSelector[{1 -> Dynamic@newstack[t, 1], 
    2 -> Dynamic@newstack[t, 2], 3 -> Dynamic@newstack[t, 3]}, slide, 
   Alignment -> Center, ImageSize -> 540]
  , FrameMargins -> 40, RoundingRadius -> 5]]

Method 2:

DynamicModule[{stack, newstack, last = 0, t, cycles = 0},
 newstack[s_] /; s == 1 && last != 1 := (If[EvenQ[cycles],
    With[{wd = RandomWord[3],
      no = RandomInteger[100, 3],
      co = RandomColor[3]},
     stack = 
      Table[Style[Row[{wd[[i]], no[[i]], co[[i]]}, Spacer[6]], 
        36], {i, 3}]
     ]
    ];
   last = s;
   ++cycles;
   stack[[s]]);
 newstack[s_] := (last = s; stack[[s]]);
 newstack[1];
 Framed[
  Pane[Dynamic@newstack[Ceiling[Clock[3, 6]]], Alignment -> Center, 
   ImageSize -> 540]
  , FrameMargins -> 40, RoundingRadius -> 5]]

I suppose the key fix here is to put the sildes themselves inside a Dynamic[] so that they will be updated when there's a change. Another key might be to update the stack in the function that fetches a slides. The reason for it is that you cannot control which Dynamic is updated first when multiple updates are triggered (as far as I recall). (A change in slide number could cause the stack to change, perhaps triggering an update of the slide on screen again both before and after the new stack is computed, a fraction of a second apart.)

Another method would be to let the clock run forever (Clock[Infinity]) and use Mod and Ceiling to map to slide number. Then the value of the clock can be used to determine when it's time to change the stack.

POSTED BY: Updating Name
Posted 3 months ago

Thank you. Both of these solutions work well. Though I've been using the Wolfram Language for about a decade, I think I have a blind spot when it comes to Dynamic. Thank you again. : )

POSTED BY: Mark Greenberg
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract