I have a little image slideshow app where it cycles through images,
showing one every few seconds. I allow for skipping forward or back by
pressing arrow keys and such. When one skips ahead, I want the timer
to reset, so that if you switch from an image near the end of its
cycle, the next image is not immediately switched.
Here is how I am doing this now:'
def reset_timer
@timer.stop if @timer
@timer = every 6 do
@i += 1
if @i > @images.size
reload_images
@i = 0
end
show_image(@i)
end
end
This is a little awkward, but straightforward enough. You have to stop
the timer before assigning a new one, because apparently all timers
ever created stay around doing their timer thing even they are not
assigned to a variable.
I am wondering: is that behavior normal? Or would it be useful to have
some better timer behavior. e.g. Timer#reset that would handle what I
want without creating new objects. I have a feeling I could hack out
Timer#reset, and it seems like useful behavior.
--
Seth Thomas Rasmussen
http://greatseth.com