Instead of passing function names to OnEvent_Timer, you could implement setTimeout and use callbacks. An example implementation would be...
code:
var timers = [];
function setTimeout(func, delay) {
var timerId = "TIMEOUT_" + delay + "_" + Math.floor(Math.random()*100000000);
MsgPlus.AddTimer(timerId, delay);
timers[timerId] = func;
return timerId;
}
function clearTimeout(timerId) {
if(timerId.substr(0, 7) === "TIMEOUT")
{
MsgPlus.CancelTimer(timerId);
timers[timerId] = null;
}
}
function setInterval(func, delay) {
var timerId = "INTERVAL_" + delay + "_" + Math.floor(Math.random()*100000000);
MsgPlus.AddTimer(timerId, delay);
timers[timerId] = func;
return timerId;
}
function clearInterval(timerId) {
if(timerId.substr(0, 8) === "INTERVAL")
{
MsgPlus.CancelTimer(timerId);
timers[timerId] = null;
}
}
function OnEvent_Timer(timerId)
{
if(timerId.substr(0, 7) === "TIMEOUT")
{
timers[timerId].call(this);
clearTimeout(timerId);
}
else if(timerId.substr(0, 8) === "INTERVAL")
{
timers[timerId].call(this);
MsgPlus.AddTimer(timerId, Number(timerId.split("_")[1]));
}
}
...and an example usage would be...
code:
function OnEvent_Initialize(MessengerStart) {
[...] //do stuff
setTimeout(function () { //sleep for two seconds
[...] //do more stuff
}, 2000);
}
It's not pretty, and can get pretty deeply nested... But I think it's better than evaling stuff.