Fork me on GitHub
#hoplon
<
2016-01-17
>
chromalchemy20:01:18

How can I create a "ticker" component, that infinitely loops through a collection of strings, showing one string after another, in place, to a timer ? with-interval

chromalchemy20:01:04

I basically want to build a simple slideshow, but combining the state of a timer with clj sequence functions is bending my mind 🙃

chromalchemy20:01:06

Something like: (recur (fnext (cycle [a b c])))?

chromalchemy20:01:12

I would like to use this timer to hide/show/animate images, so it wouldnt do to just swap an image-url string.. (need to preload?)

chromalchemy20:01:51

I imagine I might need a formula cell that tests "true" when the timer hits an interval...

chromalchemy20:01:12

I know there must be some simple elegant javelin-flavored way...🤔

micha22:01:52

@chromalchemy: you can use the clojure cycle function to create an infinite cyclic list

micha22:01:55

like this

micha22:01:30

(defc things (cycle ["foo" "bar" "baz"]))

micha22:01:52

then you can make a formula cell that contains the value of the first item, like this:

micha22:01:09

(defc= current-thing (first things))

micha22:01:22

now if you want to cycle through the things periodically you can do this:

micha22:01:47

(with-interval 1000 (swap! things rest))

micha22:01:50

and that's it!

micha22:01:06

you can see it updating with a pr formula:

micha22:01:18

(cell= (pr :current-thing current-thing))

micha22:01:00

you should see in the js console something like:

micha22:01:39

:current-item "foo"
:current-item "bar"
:current-item "baz"
:current-item "foo"
:current-item "bar"
...

micha22:01:50

continuing forever

micha22:01:36

you can change it to loop over a new set of things like this whenver you get new things to loop over:

micha22:01:58

(reset! things (cycle new-vector-of-things))

micha22:01:12

the rest of the program will automatically use the new things

micha22:01:39

you can also start and stop the slideshow:

micha22:01:49

(defc running? true)

micha22:01:59

and modify the with-interval expr:

micha22:01:23

(with-interval 1000 (when @running? (swap! things rest)))

alandipert23:01:59

I would probably keep the things in a vector and then swap! On an index cell

alandipert23:01:25

Then make a formula cell that gets from the vector using the index

alandipert23:01:45

Or maybe rotate the vector on swap.. And a formula points at the first item. Probably better, no need to check bounds