When I study the Prototype source code, I find in some occasions setTimeout is used to process the succesive operation. Like, [code] // Element scope: update: function(element, html) { html = typeof html == ''undefined'' ? '''' : html.toString(); $(element).innerHTML = html.stripScripts(); setTimeout(function() {html.evalScripts()}, 10); return element; }, replace: function(element, html) { element = $(element); html = typeof html == ''undefined'' ? '''' : html.toString(); if (element.outerHTML) { element.outerHTML = html.stripScripts(); } else { var range = element.ownerDocument.createRange(); range.selectNodeContents(element); element.parentNode.replaceChild( range.createContextualFragment(html.stripScripts()), element); } setTimeout(function() {html.evalScripts()}, 10); return element; }, [/code] Why the "setTimeout(function() {html.evalScripts()}, 10);" is used? Why do not directly do "html.evalScripts()"? What''s the intention and benefits of using that technique? Thanks in advance! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Jul 19, 6:25 am, Chris Ahn <cod...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > [snip] > > Why the "setTimeout(function() {html.evalScripts()}, 10);" is used? > Why do not directly do "html.evalScripts()"? What''s the intention and > benefits of using that technique? > > Thanks in advance!Well, there may be many reasons to use setTimeout in a chain of execution. As I understand it, here are some of them : 1) Javascript is a single threaded language, which means that the browser will "hang" as long as there are functions being called in the stack of events. Then, since eval() does not return until the string is interpreted, using setTimeout will give the browser some time to respond to events. 2) To prevent infinite loops or non responsive scripts, browsers will implement a timeout warning that will let the user "stop" it''s execution so the browser may respond to events again. Thus, using setTimeout between calls of time consuming functions will keep the browser from popping up that warning. 3) When updating the DOM, some browsers requires some time to apply the changes made to the document tree, and this will not happen while some script is executed. Therefore, setTimeout, even for 10 milliseconds, will give the browser enough time to do so, then come back to script execution. The function eval() is a time consuming function, so it''s execution is better executed separatly. Then again, setTimeout should not be used at large (like any other tweaks and tricks) as it delsys execution of functions. Anyway, some people will probably explain this better than I do, but I think this sums up the most of it. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On 7月19日, 下午9时00分, Yanick <yanick.roc...@gmail.com> wrote:> Well, there may be many reasons to use setTimeout in a chain of > execution. As I understand it, here are some of them :Thank for your wonderful explaination. Really helpful. And I suspect that whether it is intended for avoiding function stack overflow in the case that too many functions are pushed into stack, but none gets chance of being popped out, since the parent function keeps waiting for the child to finish but the child has invoked another? I wonder whether javascript handles functions like that, so I''m not sure whether my thought is right? If factual, does it easily happen? Will anyone please tell me what''s the maximal allowed depth of the stack? I frequently come across with the error "Stack overflow at line 0" (IE6 does, but firefox), I have no idea which stack it meant to. In my app, I''ve never use setTimeout to invoke a next function, is it the reason why that error occurs? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Jul 19, 12:20 pm, Chris Ahn <cod...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> [...] > > And I suspect that whether it is intended for avoiding function stack > overflow in the case that too many functions are pushed into stack, > but none gets chance of being popped out, since the parent function > keeps waiting for the child to finish but the child has invoked > another?I know not any language that do otherwise. The concept of a stack is very primitive, which is why there is an SP (stack pointer) register in the CPU Even Java, with it''s thread functionality use it... but where going off topic here.> I wonder whether javascript handles functions like that, so > I''m not sure whether my thought is right? If factual, does it easily > happen? Will anyone please tell me what''s the maximal allowed depth of > the stack?The stack size depends on the browser''s implementation I guess. You could do a simple test to see the outcome : // untested code, but you may get the idea... var count = 0; var _fn = function() { count++; _fn(); }; try { _fn(); } catch (ex) { } alert( "Stack overflow at " + count + " recursions" );> I frequently come across with the error "Stack overflow at line > 0" (IE6 does, but firefox), I have no idea which stack it meant to. In > my app, I''ve never use setTimeout to invoke a next function, is it the > reason why that error occurs?An overflow may be occuring for many reasons. The function setTimeout will not prevent an infinite loop from happening, but will prevent the stack from overflowing. But either way, this is not good ! If you have a stack overflow, do not use setTimeout ! Find out why your script throw this exception. I doubt that stacking up over 1000 functions calls in a script is good anyway... because there is no good reason why a function should call itself back. Use a timer (Prototype''s PeriodicalExecuter -- in fact, take a look how this is implemented in the library) Finally, like I said, setTimeout will slow down the execution of your scripts if you use it everywhere. Be smart about it. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Peters
2007-Jul-20 19:10 UTC
Re: Why use ''setTimeout'' to invoke the next function?
Yanick wrote:> because there is no good reason > why a function should call itself back.Well, unless you''re doing a recursive algorithm, and then you kinda need the function to call itself :) -- Michael Peters Developer Plus Three, LP --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Hi, I have tried the code you provided. Like: //sightly revised var count = 0; var _fn = function() { count++; _fn1(); }; var _fn1 = function() { count++; _fn(); }; try { _fn(); } catch (ex) { alert(ex.description || ex); alert( "Stack overflow at " + count + " recursions" ); } Interestingly, my IE reports "at 1126 recursions" and Firefox does exactly "at 1000 recursions". Both are contant no matter two functions invoke alternately or a single one invoke itself. I suspect that, in IE, the ''1126'' varies according to the specific machine but Firefox keeps a fixed scheme. Besides, the ''ex'' throwed is very the "Stack overflow"(The text showed is in my local language, which means that), and firefox reports it as "Internal error: too much recursion".>If you have a stack overflow, do not use setTimeout ! Find out why your script >throw this exception.Yes. However, my problem is that IE automatically reports the error "stack overflow at line 0" (notice that it is at "line 0", which part of my code is at line 0? And it is not alerted using try-catch and alert(), purely automatically pops out. Also, it is showed in English, not in my local language like above.). Moreover, it is strange that it happens only sometimes, though most of the time it is ok. I have no means to know which time the error will occur and to debug a program which often executes properly. Aonther point is that when my program was very small, the error had never occurred, but when it growed into a considerable large scale, the error came sometimes. It is so boresome that I can only ascribe it to IE''s bugs since in Firefox it never happens. That is the reason why I suspect function stack has relationship with my problem and wish setTimeout can solve it. It seems going off our topic. Lastly, if anybody could please provide any suggestion for my problem, I will be very grateful. Thanks anyway! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On 20 juil, 15:10, Michael Peters <mpet...-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote:> Yanick wrote: > > because there is no good reason > > why a function should call itself back. > > Well, unless you''re doing a recursive algorithm, and then you kinda need the > function to call itself :) >Indeed, though if you test a recursive algorithm against a queue (or something else) algorithm, you''ll find out that recursive is hell slow. Thus should be avoided when scripting (if at all). I''m not throwing off recursive algorithms, as I use it myself for some tasks, but only when I know how deep the recursion will go. Nothing should be used for everything :) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On 21 juil, 07:31, Chris Ahn <cod...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> [...] > > Yes. However, my problem is that IE automatically reports the error > "stack overflow at line 0" (notice that it is at "line 0", which part > of my code is at line 0? And it is not alerted using try-catch and > alert(), purely automatically pops out. Also, it is showed in English, > not in my local language like above.). Moreover, it is strange that it > happens only sometimes, though most of the time it is ok. I have no > means to know which time the error will occur and to debug a program > which often executes properly. Aonther point is that when my program > was very small, the error had never occurred, but when it growed into > a considerable large scale, the error came sometimes. It is so > boresome that I can only ascribe it to IE''s bugs since in Firefox it > never happens. > > That is the reason why I suspect function stack has relationship with > my problem and wish setTimeout can solve it. It seems going off our > topic. Lastly, if anybody could please provide any suggestion for my > problem, I will be very grateful. Thanks anyway!I''m not sure if this will help, but have you tried window.onerror ? (http://developer.mozilla.org/en/docs/DOM:window.onerror) The "line 0" could be because of many reasons... one I can think of is the use of eval() to execute some scripts. Sorry if I can''t be much more of assistance in this issue, but when something odd happens in my code, I usually walk step by step, butting try...catch at some key points. It''s a long process, but if your program is structured, you''ll be able to find the problem quick enough. Anyway. Good luck ! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---