What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » So is it impossible to make a Sleep() function?

Pages: (2): « First [ 1 ] 2 » Last »
2 votes - 3 average   So is it impossible to make a Sleep() function?
Author: Message:
segosa
Community's Choice
*****


Posts: 1407
Reputation: 92
Joined: Feb 2003
O.P. So is it impossible to make a Sleep() function?
Someone on the IRC channel asked if JS had a Sleep() function to pause the script for x milliseconds, and apparently it doesn't. I tried to make my own function, but the problem is since it's not multithreaded it causes everything to freeze for the duration that you pause the script.

My function is the following:

code:
function Sleep(ms)
{
    var x = Interop.Call("Kernel32", "GetTickCount");
    var y = x;
    while (y + ms > x)
    {
        x = Interop.Call("Kernel32", "GetTickCount");
    }
}

Calling the Sleep() API with Interop.Call does the same.

Is it possible?

This post was edited on 06-26-2006 at 10:39 PM by segosa.
The previous sentence is false. The following sentence is true.
06-26-2006 10:38 PM
Profile PM Find Quote Report
mathieumg
Full Member
***


Posts: 181
Reputation: 2
34 / Male / Flag
Joined: May 2004
RE: So is it impossible to make a Sleep() function?
Like you said, as it is not multithreaded I don't think it is possible. That's why Patchou created timers.
Official MessengerPlus! Live French Translator
Official StuffPlug 3 French Translator

:)
06-26-2006 11:07 PM
Profile E-Mail PM Web Find Quote Report
segosa
Community's Choice
*****


Posts: 1407
Reputation: 92
Joined: Feb 2003
O.P. RE: So is it impossible to make a Sleep() function?
Hmm yeah of course, could probably make something using timers...
The previous sentence is false. The following sentence is true.
06-26-2006 11:13 PM
Profile PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: So is it impossible to make a Sleep() function?
Each time I needed a sleep() in programs I made, there were always better ways to do this.

What I mean is, you can almost always make a program without the use of a sleep() or the likes. The use of sleep() is almost always not needed at all and often is the result of a bad made routine... at least that's my experience of it.

(there are some uses for it of course, but they are rare)

HO
.-= A 'frrrrrrrituurrr' for Wacky =-.
06-26-2006 11:48 PM
Profile PM Find Quote Report
Riveck
New Member
*


Posts: 1
Joined: Jun 2006
RE: So is it impossible to make a Sleep() function?
You could try to do something like this:

/* Global Var */
var flag = false;


/* Wherever you want */
[...]
MsgPlus.AddTimer("MyTimer",5000);
while(flag == false) {}
[...]


/* The timer */
OnEvent_Timer(TimerId)
{
    if (TimerId == "MyTimer")
    {
        flag=true;
        MsgPlus.AddTimer("MyTimer",5000);
    }
}
06-27-2006 12:14 AM
Profile E-Mail PM Find Quote Report
b0rna
New Member
*


Posts: 11
Joined: Jun 2006
RE: RE: So is it impossible to make a Sleep() function?
quote:
Originally posted by Riveck
You could try to do something like this:

/* Global Var */
var flag = false;


/* Wherever you want */
[...]
MsgPlus.AddTimer("MyTimer",5000);
while(flag == false) {}
[...]


/* The timer */
OnEvent_Timer(TimerId)
{
    if (TimerId == "MyTimer")
    {
        flag=true;
        MsgPlus.AddTimer("MyTimer",5000);
    }
}



This wont work, i thought of this and tried several methodes to achieve this perior to your post. once you enter the while loop you cannot break early, even the onEvent timer waits for the loop to end and thus it runs indefinatly.

I really need a way to pause my script and have yet to find one.

edit: after 3 hours of trying today i think it just might be impossible.

This post was edited on 06-27-2006 at 12:43 AM by b0rna.
06-27-2006 12:42 AM
Profile E-Mail PM Find Quote Report
TheBlasphemer
Senior Member
****

Avatar

Posts: 714
Reputation: 47
36 / – / –
Joined: Mar 2004
RE: So is it impossible to make a Sleep() function?
Erm... why on earth loop through GetTickCount when there's a win32 Sleep function ;)?
[Image: theblasp.png]
06-27-2006 01:16 AM
Profile PM Find Quote Report
b0rna
New Member
*


Posts: 11
Joined: Jun 2006
RE: So is it impossible to make a Sleep() function?
show us master for i am a nub
06-27-2006 01:28 AM
Profile E-Mail PM Find Quote Report
-dt-
Scripting Contest Winner
*****

Avatar
;o

Posts: 1819
Reputation: 74
35 / Male / Flag
Joined: Mar 2004
RE: So is it impossible to make a Sleep() function?
why would you ever want to while loop to pause your code :( just pass a function and get the function which handles the timer to call it when it is up and you can use closures to pass variables

eg

code:
var callback;
function myCode(arg1){
    //do stuff
   
    //oh noes now i need to sleep to wait for a value
   
    //function to call when timer is up
   
    function returnCallback(arg1){
        function callback(arg){
            return arg1 * arg;
        }
    return callback;
    }
    callback = returnCallback(5);
    MsgPlus.AddTimer("MyTimer",5000);
   
}



function OnEvent_Timer(TimerId){
    if(TimerId == "MyTimer"){
    //it should show the result of 5*5
    Debug.Trace(callback(5));
    }
}





more on javascript closures here
http://jibbering.com/faq/faq_notes/closures.html

This post was edited on 06-27-2006 at 02:39 AM by -dt-.
[Image: dt2.0v2.png]      Happy Birthday, WDZ
06-27-2006 02:36 AM
Profile PM Web Find Quote Report
segosa
Community's Choice
*****


Posts: 1407
Reputation: 92
Joined: Feb 2003
O.P. RE: RE: So is it impossible to make a Sleep() function?
quote:
Originally posted by TheBlasphemer
Erm... why on earth loop through GetTickCount when there's a win32 Sleep function ;)?


Read my post, I said I tried that too.
The previous sentence is false. The following sentence is true.
06-27-2006 07:11 AM
Profile PM Find Quote Report
Pages: (2): « First [ 1 ] 2 » Last »
« 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