Jonathan Borgne

Jonathan Borgne

  • NA
  • 5
  • 2.9k

Help making an array of timers

Mar 27 2012 5:21 PM
im currently having a problem trying to come up with a good approach for adding / removing
timers on the fly.

my current working code is on pastebin
http://pastebin.com/LEWkC5E8 

What it is for is I have embedded js that is ran
inside of a c# class. I have the app talking the the JS already.

Im trying to make support for calling a javascript function once the timer elapses.
This is defined in the javascript call to setTimer(interval,callback,arguments)


What I want to do is  every time setTimer() is called (will only be from javascript)
I want to create a new (one time) timer that will delete itself after it has elapsed and calls its callback.

my code does not even try to do this,
I know I will have to use an array of timers just not sure how as I'm new to c#
The code I posted will only work if the setTimer is not used more than once before
the timer elapses. Otherwise it still works but the event handler from the first call is removed and the second one is only called.
The other issue is it restarts the timers when setTimer() is called,
thus making the first call to it useless.

I was thinking there is some way using a class that will add/remove timers on the fly
and still be able to support the  [ tryCallBack(cb, "('" + arg + "');"); ]  as this is used to call
a callback defined by the js.

this is the javascript code i used to test this against so you get the idea.
when using it like this only the second timer works.



function cb(arg){
        nprint(arg);
};

setTimer(3000,'cb','hi from cb');
setTimer(1000,'cb','hi from cb2');



I was hoping to to get an example of away around this so i can finish implementing the timer functions.

thanks for any advice you can give and please keep in mind im pretty new to c# or even manage code in general.

Answers (4)