Timers in Silverlight or Javascript?

    General discussion

  • So I found that you could create an empty Storyboard, set the duration and by adding a "Completed" delegate, you can do something then restart then storyboard.

    But this feels like it's almost identical to SetTimeout.  Any reason to choose one over the other?  I suspect the storyboard may be less performant, merely because it is new, but I could be wrong??



    Friday, August 24, 2007 5:48 PM

All replies

  • In 1.1 currently, the only way to do timers is with a storyboard, so that's why you see that method frequently. As for which is better in 1.0 I don't really know.

    Friday, August 24, 2007 5:50 PM
  • I thought people used a HMLTimer in 1.1??  I haven't done too much 1.1, but I'm sure thats in there ?

    Friday, August 24, 2007 6:12 PM
  • This is reason enough for me to use the storyboard method.  I think I read somewhere that the HtmlTimer is going to be replaced somewhere down the line.

    Friday, August 24, 2007 6:16 PM
  • Maybe that was it...I know there was a reason why they said to use storyboards in 1.1...

    Friday, August 24, 2007 6:28 PM
  • THis shows a workaround by using an Animation Timer:

    First set up an Animation Timer in your xaml...

    Storyboard x:Name="timer">
    DoubleAnimation Duration="00:00:0.02" />

    Then you can add an "tick" event handler and start up the timer..

    timer.Completed += new EventHandler(timer_Completed);

    And finally, make sure you restart the timer in the timer_Completed event.

    void timer_Completed(object sender, EventArgs e)
       // ... do per tick stuff here...

       // restart the timer

    Tuesday, August 28, 2007 7:02 AM
  • And finally, make sure you restart the timer in the timer_Completed event.

    Actually, it's probably better design to set the RepeatBehavior of the Storyboard to "Forever".  That will probably be a more accurate Timer than restarting it in the Completed event.  Then, if you choose, you can Stop() the Storyboard, in the Completed event, based on some piece of data.

    Tuesday, August 28, 2007 10:18 AM
  • Oh I wasn't aware that the Completed event still fires if the RepeatBehavior is forever. I'l have to try this.

    Tuesday, August 28, 2007 10:30 AM