Stepping through GM_xmlhttpRequest

in Script development
Subscribe to Stepping through GM_xmlhttpRequest 13 posts, 8 voices



davidseguin Scriptwright

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 if (responseDetails.status == 200 && responseDetails.statusText == "OK" && responseDetails.readyState == 4 && responseDetails.responseHeaders) but that doesn't slow it down at all.

Does anyone have any ideas for me?

 
dob Scriptwright

Why not launch the functions recursively?

Something like this:

function handle_response(counter, response) {
	alert(response);
	if (counter < 100) {
		GM_xmlhttpRequest({
			method: "get",
			url: url,
			onload: function(e) {
				handle_response(++counter, e) {
			});
		}
	} else return false;
}

Basically you launch the next GM_xmlhttpRequest in its callback function thus ensuring it's finished.

 
JoeSimmons Scriptwright

I had this problem once... avg fixed it for me... it had something do to with a while() loop.

 
smk Scriptwright

yeah, I guess javascript is not multi-threaded

 
Avindra V.G. Scriptwright

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.

 
dob Scriptwright

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.

 
littlespark Scriptwright

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.
 
jerone Scriptwright

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();

 
sizzlemctwizzle Scriptwright

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.

 
littlespark Scriptwright

This might be the solution you are looking for:


// some codes specifically for the above script
// not needed here any longer

 
davidseguin Scriptwright

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.

 
davidseguin Scriptwright

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.

 
littlespark Scriptwright

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.
Cross
Presentational HTML allowed.
Use <code> for inline code and <pre> for code blocks. Use &lt; and &gt; for literal < and >.
We help break paragraphs and link your links.
or cancel