Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Javascript pause or sleep thread execution

129 views
Skip to first unread message

jsuer

unread,
Dec 11, 2009, 1:34:47 PM12/11/09
to
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/

Jorge

unread,
Dec 11, 2009, 2:35:19 PM12/11/09
to
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.

JR

unread,
Dec 11, 2009, 3:49:07 PM12/11/09
to

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

9.8 How do I make a 10 second delay?

Cheers,
JR

Richard Maher

unread,
Dec 11, 2009, 5:14:54 PM12/11/09
to

"jsuer" <us...@compgroups.net/> wrote in message
news:5tOdnSouzf9aDb_W...@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


Mike Duffy

unread,
Dec 11, 2009, 10:48:00 PM12/11/09
to
Jorge <jo...@jorgechamorro.com> wrote in news:78c6df2c-2b08-4e37-b33a-
07c66a...@z41g2000yqz.googlegroups.com:

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;}

Jorge

unread,
Dec 12, 2009, 5:05:24 AM12/12/09
to
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.

0 new messages