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