On Thu, May 26, 2011 at 10:57 PM, Russellmd <[email protected]> wrote:
> I have a "for" instruction, for each iteration is executin a request(I > used XHR). Each request access an external url, so execution time is > about 4-6 seconds, and I need to access at least 600 external links. > By the way: I wonder how come you don't get a 'slow running script, abort or continue' dialog from your web browsers yet. If you actually need what you say you're doing, than we're talking about nontrivial work to do here. Therefor I'll assume that you know what you're doing on all other counts and take it from there. > I would like to display result of each request, one by one. At the > moment my script is executing and show me all items when "for" is > finished. So, it is possible to display result of each iteration > immediatly , when iteration is finished? > Simple: the data becomes visible in the browser as soon as you update the DOM in the Request's onComplete event handler. (With the special note: as long as your JS script is running, you won't *see* anything --> If you cut your run into several small-time pieces, the browser will get the needed 'air' to actually do the screen updates. If you do what's written below, you'll see it happen.) (And don't forget to consider what to do when the connection FAILS: I've had your sort of scenario before and some servers WILL consider this being a DoS attack and shut you down: that means you won't even be receiving a 4xx or 5xx HTTP error code, at the least. [*] Which leads me to...) Since this is a lot of work and you are hammering the server(s) you need to do some serious work to make this 'well behaved' and work for you (irrespective of you using mootools or something else: your crossing several 'sane' boundaries in here): 1) for the 'slow running script, abort or continue' to go away, you need to split up the work into multiple runs. You can do long running JS ops like that by doing a bit of work, then use a timer (e.g. by calling a function with .delay(10) appended) to run the next chunk. Continue until done. 2) for the server hammering, you WILL need to queue the requests (there's mootools Request.Queue for that) to control the maximum number of simultaneous connections while you're doing this. A (complex) example of this activity can be found in mootools-filemanager (see github; FileManager.js, look for the 'fill_chunkwise_1' function in that code; this is ABSOLUTELY NOT for beginners as there's additional 'smarts' in there re the 'long running script' prevention and lots of other (irrelevant to this subject) stuff as well. Basically what happens in there is taking care of unknown browser/client machine performance by _timing_ the current run (something you should do too) and determining when it is _time_ to end the current piece (== loop) of JS execution and go and invoke func.delay(t); by not working with (semi)hardcoded number-of-rounds we can run well on both slow and fast machines. The Request.Queue stuff is also used in there, but that's maybe 5 lines of code in several K so not for the faint of heart and NOT suitable as 'easy example how to use this'; see the mootools.More Request.Queue documentation for easily accessible information instead. (MTFM has this to be able to serve directories with several 100's to even 1000's of media/images per directory to web interface users. Of course, that means there's much more in there than just this, if you want to make it work in both intranet and internet settings) footnotes on failure path considerations: [*] ... and when you use Request.JSON or derivatives, you'll note there's both OnFailure and OnError. Both serve different failure paths: one is for handling AJAX parse failures (which at least means you received *something*, MAYBE even a 200 HTTP response code alongside ;-) ); the other is for handling HTTP error responses like 4xx/5xx and other network failures. Make sure you mind them both, but consider that remark part of a general and permanent 'programmers should mind about failure paths!' rant of mine. :-) -- Met vriendelijke groeten / Best regards, Ger Hobbelt -------------------------------------------------- web: http://www.hobbelt.com/ http://www.hebbut.net/ mail: [email protected] mobile: +31-6-11 120 978 --------------------------------------------------
