From: jsuer on
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/
From: Jorge on
On Dec 11, 7:34 pm, jsuer <u...(a)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.
From: JR on
On Dec 11, 4:34 pm, jsuer <u...(a)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
From: Richard Maher on

"jsuer" <user(a)compgroups.net/> wrote in message
news:5tOdnSouzf9aDb_WnZ2dnUVZ_sCdnZ2d(a)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


From: Mike Duffy on
Jorge <jorge(a)jorgechamorro.com> wrote in news:78c6df2c-2b08-4e37-b33a-
07c66a956e4c(a)z41g2000yqz.googlegroups.com:

> On Dec 11, 7:34�pm, jsuer <u...(a)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;}