【发布时间】:2012-04-27 00:17:09
【问题描述】:
我有一个相当简单的任务来替换页面上的特定文本。假设有 10 个 DIV,每个 DIV 都包含特定信息。我想对照数据库检查这些信息,然后用数据库中的结果替换文本。
我将 GM_xmlhttpRequest 放在一个循环中,该循环应该检查此信息并为 10 个 DIV 中的每一个替换它。不幸的是,这不起作用,最后一个 DIV 包含 10 个完全相同的信息。也就是说,当 i=10 时,GM_xmlhttpRequest 被执行了 10 次。
下面是一个简化的代码:
var i=1;
while (i<=10) {
var id = 'div-' + i; //This sets the name of the DIV
var ShowContent = document.getElementById(id);
GoToURL = "http://domain.com/?=" + id; //This specifies a URL that changes on every loop
GM_xmlhttpRequest({
method: "GET",
url: GoToURL,
onload: function(response) {
ShowContent.innerHTML = response; //This should replace the information for each DIV
alert(i); //TEST: this shows always 11 (even not 10!). Why?
}
});
i++;
)
【问题讨论】:
标签: javascript while-loop greasemonkey fetch gm-xmlhttprequest