schedule() & cancel()
by Joseph Davidson · 12/29/2008 (11:20 am) · 1 comments
// usable multi-timer //
function timer(%index, $timer, %val)
{
%val++;
$timer[%index] = schedule(1000, 0, timer, %index, $timer, %val);
return %val;
}
// multi-timer with echo check //
function timer(%index, $timer, %val)
{
%val++;
$timer[%index] = schedule(1000, 0, timer, %index, $timer, %val);
echo(%val);
return %val;
}
function timer(%index, $timer, %val)
{
%val++;
$timer[%index] = schedule(1000, 0, timer, %index, $timer, %val);
return %val;
}
// multi-timer with echo check //
function timer(%index, $timer, %val)
{
%val++;
$timer[%index] = schedule(1000, 0, timer, %index, $timer, %val);
echo(%val);
return %val;
}
Associate Orion Elenzil
Real Life Plus
there are a few things which i think are a bit funky with this code.
first,
timer() returns a value, but whom is it returning the value to ?
answer: nobody, because it's schedule itself which will be calling timer().
second,
there's no point in passing a global variable: it's global.
third,
unfortunately arrays in torquescript are not really first-class objects, and you can't pass them around. look at a resource like this, or better, this for more info.
regarding your question about how to store all the timer IDs,
putting them in a global array seems like a fine approach to me.
it's certainly one of the more efficient ways available from torquescript.
here is some code which might be a version of the same thing you're trying to do:
function cancelTimer(%timerName) { %timerID = $gTimerIDs[%timerName]; if (%timerID !$= "") { cancel(%timerID); } } function repeatTimer(%timerName, %interval, %callbackFunction, %callbackData) { cancelTimer(%timerName); // when using a repeating schedule, always attempt to cancel the previous one first. %timerID = schedule (%interval, 0, "repeatTimer", %timerName, %interval, %callbackFunction, %callbackData); $gTimerIDs [%timerName] = %timerID; $gTimerCounts[%timerName] += 1; call(%callbackFunction, %callbackData, $gTimerCounts[%timerName]); } //------------------------------- function exampleCallback(%data, %count) { echo("hello! callback data =" SPC %data SPC " count =" SPC %count); }example usage:
==>repeatTimer("myTimerA", 1000, "exampleCallback", "A"); hello! callback data = A count = 1 hello! callback data = A count = 2 hello! callback data = A count = 3 hello! callback data = A count = 4 hello! callback data = A count = 5 hello! callback data = A count = 6 hello! callback data = A count = 7 hello! callback data = A count = 8 hello! callback data = A count = 9 ==>repeatTimer("myTimerB", 2000, "exampleCallback", "B"); hello! callback data = B count = 1 hello! callback data = A count = 10 hello! callback data = A count = 11 hello! callback data = B count = 2 hello! callback data = A count = 12 hello! callback data = A count = 13 hello! callback data = B count = 3 ... hello! callback data = A count = 26 hello! callback data = A count = 27 hello! callback data = B count = 10 hello! callback data = A count = 28 hello! callback data = A count = 29 ==>cancelTimer("myTimerA"); hello! callback data = B count = 11 hello! callback data = B count = 12 hello! callback data = B count = 13 hello! callback data = B count = 14 ==>cancelTimer("myTimerB");