Timeout Objects - Printable Version
-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Timeout Objects (/showthread.php?tid=92297)
Timeout Objects by trevorpe on 09-19-2009 at 11:35 PM
Hi there.
Is there anyway of doing the setTimeout() function in MsgPlus?
I need to pass an argument but you cannot do that with MsgPlus.AddTimer().
I tried it and it doesn't know about it because it says Object Expected.
RE: Timeout Objects by roflmao456 on 09-20-2009 at 01:08 AM
I dunno, maybe eval() will work?
code: function somefunction(param){
Debug.Trace(param);
}
function OnEvent_Timer(TimerId){
eval(TimerId);
}
MsgPlus.AddTimer("somefunction(\"test\")", 1000);
RE: Timeout Objects by Matti on 09-20-2009 at 06:54 AM
If you really want setTimeout functionality, you could try this class I made for my scripts. You have to note though that you can't use "normal" MsgPlus.AddTimer() calls unless you edit the OnEvent_Timer in this class.
Example usage:
js code: function somefunction(message) {
Debug.Trace("Timer reached, message received: "+message);
}
var myTimer = new Timer(1000, somefunction, "test");
//Available methods:
myTimer.Refresh(); // Refreshes timer (stops and restarts)
myTimer.Cancel(); // Destructor
js code: /*
File: Timer.js
Desc: Class for timer handling
*/
var Timers = {};
var nTimersCount = 0;
var Timer = function(nInterval, fCallback, oParam) {
this.Interval = nInterval;
this.Callback = fCallback;
this.Index = nTimersCount++;
if(typeof oParam !== "undefined") this.Param = oParam;
Timers[this.Index] = this;
MsgPlus.AddTimer('Timer'+this.Index, this.Interval);
}
Timer.prototype = {
"Refresh" : function() {
MsgPlus.AddTimer('Timer'+this.Index, this.Interval);
},
"Cancel" : function() {
MsgPlus.CancelTimer('Timer'+this.Index);
delete Timers[this.Index];
}
}
function OnEvent_Timer(sTimerId) {
if(/^Timer(\d+)$/.test(sTimerId)) {
var nIndex = 1*RegExp.$1;
if(typeof Timers[nIndex].Param === "undefined") Timers[nIndex].Callback(Timers[nIndex]);
else Timers[nIndex].Callback(Timers[nIndex], Timers[nIndex].Param);
}
}
RE: Timeout Objects by trevorpe on 09-20-2009 at 11:23 AM
Hi there guys.
Thanks Matti!!
I didn't use your exact class. I made my own custom object and changed a few things so I could pass multiple parameters.
But your Idea helped me a lot. Thanks again. Here's what I wrote. Please tell me if you find something wrong.
code: //This is in TimerFunction.js
//Do Not Edit
var Timers = new Array();
var TimersIndex = 0;
function myTimer(FunctionName, Elapse, Parameter, Parameter2, Parameter3, Parameter4, Parameter5) {
this.FunctionName=FunctionName
this.Elapse=Elapse;
this.TimerIndex=TimersIndex++;
this.Refresh=TimerRefresh;
this.Delete=TimerDelete;
FunctionText=FunctionName + "(";
//Set all parameters
if (typeof Parameter !== "undefined") {
this.Param1=Parameter;
FunctionText+="TimerObj.Param1";
}
if (typeof Parameter2 !== "undefined") {
this.Param2=Parameter2;
FunctionText+=", TimerObj.Param2";
}
if (typeof Parameter3 !== "undefined") {
this.Param3=Parameter3;
FunctionText+=", TimerObj.Param3";
}
if (typeof Parameter4 !== "undefined") {
this.Param4=Parameter4;
FunctionText+=", TimerObj.Param4";
}
if (typeof Parameter5 !== "undefined") {
this.Param5=Parameter5;
FunctionText+=", TimerObj.Param5";
}
this.FunctionText = FunctionText + ")";
//Set Object
Timers[this.TimerIndex]=this;
MsgPlus.AddTimer("myTimer"+this.TimerIndex.toString(), this.Elapse);
}
function TimerRefresh() {
MsgPlus.AddTimer("myTimer"+this.TimerIndex.toString(), this.Elapse);
}
function TimerDelete() {
MsgPlus.CancelTimer("myTimer"+this.Index);
delete Timers[this.Index];
}
function OnEvent_Timer(TimerId) {
if (TimerId.substring(0, 7) === "myTimer") {
var TimerObj=Timers[TimerId.substring(7)];
eval(TimerObj.FunctionText);
}
}
RE: Timeout Objects by Matti on 09-20-2009 at 05:47 PM
Well, actually there's no need to add so many arguments. You could simply make another function which passes the right parameters and use that as callback, like so:
js code: function somefunction(arg1, arg2, arg3) {
// Do something useful
}
// Method 1: hard-coded parameters inside new function
var callback = function(timer) {
somefunction("aa", 123, 3.14159);
};
new Timer(1000, callback);
// Method 2: use array to get parameters
var callback = function(timer, args) {
// Pass each element in the args array as parameter
somefunction.apply(null, args);
};
var param = ["aa", 123, 3.14159];
new Timer(1000, callback, param);
As you can see, by using some clever coding you can even omit sending the parameter! Of course, this will make your callback function less portable, but it'll save you some precious code in your class itself.
And most important of all: you can avoid using eval() (since everybody knows that eval is evil!)
|