Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Javascript pause or sleep thread execution
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
jsuer  
View profile  
 More options Dec 11 2009, 1:34 pm
Newsgroups: comp.lang.javascript
From: jsuer <u...@compgroups.net/>
Date: Fri, 11 Dec 2009 12:34:47 -0600
Local: Fri, Dec 11 2009 1:34 pm
Subject: Javascript pause or sleep thread execution
hi,
is there a way to pause to sleep a running thread in js?
similiar to C# System.Threading.Thread.Sleep() function

---
frmsrcurl: http://compgroups.net/comp.lang.javascript/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jorge  
View profile  
 More options Dec 11 2009, 2:35 pm
Newsgroups: comp.lang.javascript
From: Jorge <jo...@jorgechamorro.com>
Date: Fri, 11 Dec 2009 11:35:19 -0800 (PST)
Local: Fri, Dec 11 2009 2:35 pm
Subject: Re: Javascript pause or sleep thread execution
On Dec 11, 7:34 pm, jsuer <u...@compgroups.net/> wrote:

> hi,
> is there a way to pause to sleep a running thread in js?
> similiar to C# System.Threading.Thread.Sleep() function

There's a single thread per window, and no, there's no way to put it
to sleep.
But a window may contain other windows in <iframe>s, and these behave
as additional, concurrent threads. See:

http://jorgechamorro.com/cljs/059/

If the browser has the Google Gears plugin installed, or if it
implements the w3c HTML -draft- webWorkers API (based on Google's
Gears API), any number of additional JS background threads can be
started as webWorkers:

http://code.google.com/apis/gears/api_workerpool.html
http://dev.w3.org/html5/workers/

But still, afaik, there's no API to programmatically halt any of these
threads at an arbitrary point in the middle of the execution flow.
Code execution can be triggered by timers and/or events, but not
temporarily suspended, it always runs up to the end of the program.
--
Jorge.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
JR  
View profile  
 More options Dec 11 2009, 3:49 pm
Newsgroups: comp.lang.javascript
From: JR <groups_j...@yahoo.com.br>
Date: Fri, 11 Dec 2009 12:49:07 -0800 (PST)
Local: Fri, Dec 11 2009 3:49 pm
Subject: Re: Javascript pause or sleep thread execution
On Dec 11, 4:34 pm, jsuer <u...@compgroups.net/> wrote:

> hi,
> is there a way to pause to sleep a running thread in js?
> similiar to C# System.Threading.Thread.Sleep() function

> ---
> frmsrcurl:http://compgroups.net/comp.lang.javascript/

I suggest reading the FAQ at
http://www.jibbering.com/faq/

9.8 How do I make a 10 second delay?

Cheers,
JR


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Maher  
View profile  
 More options Dec 11 2009, 5:14 pm
Newsgroups: comp.lang.javascript
From: "Richard Maher" <maher...@hotspamnotmail.com>
Date: Sat, 12 Dec 2009 06:14:54 +0800
Local: Fri, Dec 11 2009 5:14 pm
Subject: Re: Javascript pause or sleep thread execution

"jsuer" <u...@compgroups.net/> wrote in message

news:5tOdnSouzf9aDb_WnZ2dnUVZ_sCdnZ2d@giganews.com...

> hi,
> is there a way to pause to sleep a running thread in js?
> similiar to C# System.Threading.Thread.Sleep() function

What Thread do you wish to sleep and what do you expect to be happening in
the meantime?

You can do many such things with JAVA Applets. I currently use wait() and
notify() to implement my "synchronous" socket i/o functionality where the
EDT is blocked until your callback in turn calls the rendezvous() method.

Cheers Richard Maher


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mike Duffy  
View profile  
 More options Dec 11 2009, 10:48 pm
Newsgroups: comp.lang.javascript
From: Mike Duffy <resp...@invalid.invalid>
Date: Sat, 12 Dec 2009 03:48:00 +0000 (UTC)
Subject: Re: Javascript pause or sleep thread execution
Jorge <jo...@jorgechamorro.com> wrote in news:78c6df2c-2b08-4e37-b33a-
07c66a956...@z41g2000yqz.googlegroups.com:

> On Dec 11, 7:34 pm, jsuer <u...@compgroups.net/> wrote:
> But still, afaik, there's no API to programmatically halt any of these
> threads at an arbitrary point in the middle of the execution flow.
> Code execution can be triggered by timers and/or events, but not
> temporarily suspended, it always runs up to the end of the program.

I have not tested the following function. I found it on the web a few weeks
ago when I believed that I wanted a sleep. Subsequently, I changed my mind
about what approach would work best for me and decided on something else.

The function simply loops making repeated synchronous waits on a
nonexistant resource. This is presumably better than a loop that eats
processor time. But it might be perceived as a DOS attack. Perhaps you can
track down the location of a "tarpit" and use that as the resource address.

The advantage is that your calling function does not need to be split up
into separate before and after functions.

function doBrowserEvents(msecs)
{var i=0; var j5timerXH = initXmlHttpTimer(); var j5UTC = new Date();
while ((new Date()) - j5UTC < msecs)
  {i++; j5timerXH.open("GET","/doesnotexist",false); j5timerXH.send
(null);};
return i;}


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jorge  
View profile  
 More options Dec 12 2009, 5:05 am
Newsgroups: comp.lang.javascript
From: Jorge <jo...@jorgechamorro.com>
Date: Sat, 12 Dec 2009 02:05:24 -0800 (PST)
Local: Sat, Dec 12 2009 5:05 am
Subject: Re: Javascript pause or sleep thread execution
On Dec 12, 4:48 am, Mike Duffy <resp...@invalid.invalid> wrote:

> (...)
> function doBrowserEvents(msecs)
> {var i=0; var j5timerXH = initXmlHttpTimer(); var j5UTC = new Date();
> while ((new Date()) - j5UTC < msecs)
>   {i++; j5timerXH.open("GET","/doesnotexist",false); j5timerXH.send
> (null);};
> return i;}

Hmm, nice try, but the synchronous XHR blocks everything else (as
expected)... see:

http://jorgechamorro.com/cljs/059/

Create an [incremetator or decrementator or bubbles] and a sleeper,
and click on the "Sleep for 5s" button... :-(

The code is:

btnSleep.onclick= function (time, time2, xhr, kmiliSegundos) {
  kmiliSegundos= 5e3;
  time= +new Date();
  do {
    xhr= new XMLHttpRequest();
    xhr.open("GET", "/rAnDoM_"+ rndStr(12), false); //SYNC !!
    xhr.send(null);
    time2= +new Date();
    btnSleep.innerHTML= ((time2- time)/ 1e3) | 0;
  } while (time2 < (time+ kmiliSegundos));
  btnSleep.innerHTML= "Sleep for 5s";

};

It's not exactly the same algorithm because in the code you posted the
same XHR is reused again and again, but I tried it both ways and it
makes no difference.

Tested in Safari 4, FF 3.6, Opera 10, Chrome ß, in a Mac with OSX.
Cheers,
--
Jorge.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »