On 02/27/11 17:05, Mike Soh wrote:
.. It seems like the process is async, instead of sync...
Yes, GM_xhr is asynchronous by design.
here's the code:
for (var i = 0; i< links.length; i++) {
...
GM_xmlhttpRequest({
...
onload: function(r) {
GM_log(i + ': contacting ' + p.getAttribute('href'));
GM_log(i + ': connection status: ' + r.status);
...
});
In this case, your inner (anonymous) function has a closure to the outer
scope. The callbacks all happen asychronously, after the call has
completed, so they all point to the same "i" variable (and p), which
contains the value as after the entire loop has run to completion.
Moving the call to GM_xhr (really, the anonymous callback function
defined therein) to its own method gives each a closure to the separate
call made, thus the values will be retained.
--
You received this message because you are subscribed to the Google Groups
"greasemonkey-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/greasemonkey-users?hl=en.