---
frmsrcurl: http://compgroups.net/comp.lang.javascript/
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.
I suggest reading the FAQ at
http://www.jibbering.com/faq/
9.8 How do I make a 10 second delay?
Cheers,
JR
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
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;}
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.