我想召回GM_xmlhttpRequest,如果页面中没有回答的文本,如循环。
GM_xmlhttpRequest({
method: 'POST',
url: 'http://localhost/getcaptcha.php',
data: 'login='+login+'&password='+password,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function(responseDetails) {
if(responseDetails.responseText.length==3) {
// do something
}
else{
// i wanna go back to the GM_xmlhttpRequest again while there's no answer with the length==3
}
}
});
我该怎么做呢?
将请求代码放入函数中,并在请求失败时再次调用它。像这样:
function sendRequest(attempt)
{
// If the parameter is missing then this is our first attempt
if (typeof attempt == "undefined")
attempt = 1;
GM_xmlhttpRequest({
...
// If request failed and we tried less than three times - try again
if (attempt <= 3)
sendRequest(attempt + 1);
...
});
}
sendRequest();