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

Sleep
Author: Message:
Amec
Junior Member
**


Posts: 19
32 / Male / Flag
Joined: Sep 2008
RE: Sleep
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.

This post was edited on 01-23-2011 at 08:11 AM by Amec.
01-23-2011 08:06 AM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
Sleep - by Scale on 12-18-2010 at 08:52 PM
RE: Sleep - by mynetx on 12-18-2010 at 08:56 PM
RE: Sleep - by Scale on 12-18-2010 at 09:09 PM
RE: Sleep - by matty on 12-18-2010 at 11:47 PM
RE: Sleep - by Amec on 01-23-2011 at 08:06 AM
RE: Sleep - by Matti on 01-23-2011 at 11:02 AM


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