Re: Execute Last
"Ian Petersen" <[email protected]> Tue, 21 Mar 2006 15:18:40 -0500
| Newsgroups | gmane.comp.web.dom.wdf |
|---|---|
| Message-ID | <[email protected]> |
I can't speak to the general behaviour of the community, but for me it's a bit of voodoo. setTimeout(aFunc, 0) seems to me like a request to call aFunc immediately. I don't know what actually happens when a browser executes it, but if I want the function to be called _later_, then it feels wrong to make a request for _immediate_ execution. I guess I'm saying it's personal taste. Ian On 3/21/06, Ryan Hicks <[email protected]> wrote: > Here's a quick question about using setTimeout to asynchronize execution, > since we're on the topic. > > Recently, I've seen a lot of examples using 1ms for the time delay, but > prior to about a year ago, most examples showed 0ms. Is there a functional > difference? Did the community discover something about how the negligible > delay improved performance over the nonexistent delay? Or is 1ms just more > fashionable now and it's entirely a matter of personal choice? > > -ryan > > > -----Original Message----- > From: [email protected] [mailto:[email protected]] On Behalf Of > Moran Ben-David > Sent: Tuesday, March 21, 2006 8:02 AM > To: [email protected] > Subject: RE: [wdf-dom] Execute Last > > Much thanks to you, Ian. I suppose you transposed my initial question to > one that asks whether setTimeout results parallel execution or not. Your > answer being "not". > > Given that, setTimeout is perfect for my case. That is, it is essentially > like putting code at the end of the single execution thread. I reran my > code and it works using 1ms. > > Thanks for taking the time to answer and reformulate my example. I greatly > appreciate it! > > Moran > > > -----Original Message----- > > From: [email protected] [mailto:[email protected]] On Behalf > > Of Ian Petersen > > Sent: Tuesday, March 21, 2006 9:48 AM > > To: [email protected] > > Subject: Re: [wdf-dom] Execute Last > > > > Hi Moran, > > > > > Does anyone know of a good way to delay execution of a piece of code > > without > > > relying on time (as in setInterval and setTimeout)? > > > > Can I ask why you're concerned about relying on time? > > > > In my experience, all browsers run Javascript in a single thread, > > which means you can never have parallel execution, even if you use > > setTimeout. So changing your code like this would do the trick: > > > > var lastExecutors = [] > > var timeoutID = null > > > > function executeLast(o, f) { > > lastExecutors.push([o, f]) > > > > if(!timeoutID) { > > timeoutID = setTimeout(cycleLastExecutors, 1); > > } > > } > > > > function cycleLastExecutors() { > > timeoutID = null; > > > > var task; > > > > while(lastExecutors.length > 0) { > > task = lastExecutors.shift(); > > > > task[1].apply(task[0]); > > } > > } > > > > > > With the above code, calling executeLast(foo, foo.last) will schedule > > a call to foo.last() to be run 1 millisecond after the current thread > > of execution terminates. Is one millisecond too long for your > > application? > > > > Ian > > > > -- > > Tired of pop-ups, security holes, and spyware? > > Try Firefox: http://www.getfirefox.com > > > > > > Unsubscribe > > [email protected] > > > > List info > > http://www.quirksmode.org/dom/list.html > > Yahoo! Groups Links > > > > > > > > > > > > > > > Unsubscribe > [email protected] > > List info > http://www.quirksmode.org/dom/list.html > Yahoo! Groups Links > > > > > > > > > Unsubscribe > [email protected] > > List info > http://www.quirksmode.org/dom/list.html > Yahoo! Groups Links > > > > > > > -- Tired of pop-ups, security holes, and spyware? Try Firefox: http://www.getfirefox.com Unsubscribe [email protected] List info http://www.quirksmode.org/dom/list.html Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/wdf-dom/ <*> To unsubscribe from this group, send an email to: [email protected] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/