|
|
Here is my script: http://userscripts.org/scripts/review/39995 I'm using GM_xmlhttpRequest to load comments from the New York Times API, 25 comments at a time. Problem is, when I loop through them, some requests go faster than others, so I end up getting the comments out of order. I need something to slow this down, so the next request in the loop doesn't come until the last one is finished and rendered on the page. I tried Does anyone have any ideas for me? |
|
|
Why not launch the functions recursively? Something like this:
Basically you launch the next GM_xmlhttpRequest in its callback function thus ensuring it's finished. |
|
|
I had this problem once... avg fixed it for me... it had something do to with a |
|
|
yeah, I guess javascript is not multi-threaded |
|
|
yeah basically, you handle increments with the xmlrequest itself. if you have for or while somewhere in there, chances are you're doing it wrong. javascript is not multi-threaded actually, i think using setTimeout and setInterval, it's technically super-multi-threaded. (But do not use either of those in this particular case, refer to my suggestion above or dob's post. |
|
|
I'd go recursively. It ensures that the previous XHR is finished and it's just the most awesome way to go. In order to understand recursion, one must understand recursion. |
|
|
Found recently that the key in stepping through GM_xmlhttpRequest is using functions with arguments to control the flow and speed. Here's something i wrote: Complete Control of GM_xmlhttpRequest Might be of some reference for your coding. |
|
|
I would have put it in an array, number it, sort the array and print when last on is finished.
var arr=[[0,"blabla"],[1,"blabla"],...];
var index = 0;
GM_xmlhttpRequest({
method: "get",
url: url,
onload: function(x){
arr.push([index++,x.responseText])
});
}
arr.sort();
|
|
|
I was thinking the same. Just store the comments(each page in an array). Wait for all the pages to load and then loop through your array to add the comments to the page. Btw, I had this problem about 2 years ago and since I was just starting out at javascript I was unable to solve it. |
|
|
This might be the solution you are looking for:
|
|
|
Thanks for your ideas and responses. It's been a busy week for me, I haven't had much time to muck about with them. I will try some of these things this weekend and let you know how it works out. Thanks again. I've learned a lot from this forum. |
|
|
Little spark, thanks for your example, this is what I'm using right now. I think your solution is the most literal solution, although putting the pages into an array would probably be faster on the page. Stepping through the calls one by one requires one to finish before the next starts, whereas in the array they could come in however best the server can serve them. Thanks again to everyone here. This is my first GM script and I'm learning a lot from all of you. |
|
|
Hi david, think your script doesn't need the use of array in the GM_xmlhttpRequest, using it might be an overkill. Have learned too from this post. Thanks everyone. |