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 | 
 | 
  | 
mathieumg 
Full Member 
   
  
 
Posts: 181 Reputation: 2 
36 /   /   
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 | 
 | 
  | 
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 | 
 | 
  | 
CookieRevised 
Elite Member 
     
  
  
 
Posts: 15494 Reputation: 173 
– /   /   
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 | 
 | 
  | 
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 | 
 | 
  | 
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 | 
 | 
  | 
TheBlasphemer 
Senior Member 
    
  
  
 
Posts: 714 Reputation: 47 
38 / – / – 
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   ?  
 |   
 | 
| 06-27-2006 01:16 AM | 
 | 
  | 
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 | 
 | 
  | 
-dt- 
Scripting Contest Winner 
     
  
  
;o
  
Posts: 1818 Reputation: 74 
37 /   /   
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-.
       Happy Birthday, WDZ 
 |   
 | 
| 06-27-2006 02:36 AM | 
 | 
  | 
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 | 
 | 
  | 
| 
Pages: (2): 
« First
  
 [ 1 ]
 2
 
»
 
Last »
 | 
| 
 |