What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Timeout Objects

Timeout Objects
Author: Message:
trevorpe
New Member
*


Posts: 12
28 / Male / Flag
Joined: Dec 2008
O.P. Timeout Objects
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.
09-19-2009 11:35 PM
Profile PM Find Quote Report
roflmao456
Skinning Contest Winner
****

Avatar

Posts: 955
Reputation: 24
29 / Male / Flag
Joined: Nov 2006
Status: Away
RE: Timeout Objects
I dunno, maybe eval() will work?

code:
function somefunction(param){
Debug.Trace(param);
}

function OnEvent_Timer(TimerId){
eval(TimerId);
}

MsgPlus.AddTimer("somefunction(\"test\")", 1000);

[quote]
Ultimatess6
: What a noob mod
09-20-2009 01:08 AM
Profile PM Web Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
31 / Male / Flag
Joined: Apr 2004
RE: Timeout Objects
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:
Javascript 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

Javascript 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);
    }
}

Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
09-20-2009 06:54 AM
Profile E-Mail PM Web Find Quote Report
trevorpe
New Member
*


Posts: 12
28 / Male / Flag
Joined: Dec 2008
O.P. RE: Timeout Objects
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);
    }
}
09-20-2009 11:23 AM
Profile PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
31 / Male / Flag
Joined: Apr 2004
RE: Timeout Objects
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:
Javascript 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!)
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
09-20-2009 05:47 PM
Profile E-Mail PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On