Geo*_*F67 2 javascript greasemonkey asynchronous gm-xmlhttprequest
如下所示:
我有一个异步的脚本.我想传递一个值INTO这个函数,这样当调用onload函数时我可以用它来显示在网页中.
我遇到的挑战是,每次将其传递给函数时,此值都会发生变化.
所以,例如,如果我传入'abc','def','xyz'.
我最终会
xyz
xyz
xyz
Run Code Online (Sandbox Code Playgroud)
代替
abc
def
xyz
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是,如何将值传递给此函数,以便函数的每次调用都知道完成后要显示的内容?
您正在寻找关闭: -
var urls = {"abc": "http://somehost/aurl",
"def": "http://somehost/otherurl",
"ghi": "http://someotherhost/aurl" }
for (var k in urls)
{
GM_xmlhttpRequest({
method: 'GET',
url: urls[k],
onload: function(text) {
return function(xhr) {
//Do stuff with xhr responseText etc and the text parameter
alert(text)
}
}(k)
}
}
Run Code Online (Sandbox Code Playgroud)
在每个未完成的请求完成后,这将警告"abc","def"和"ghi".