문제

여기 에이 코드가 있습니다.

var infiltrationResult;

while(thisOption) {
    var trNode = document.createElement('tr');
    var tdNode = document.createElement('td');
    var hrefNode = document.createElement('a');

    infPlanetID = thisOption.getAttribute('value');

  var myURL = "http://www.hyperiums.com/servlet/Planetinf?securitylevel=90&newinfiltr=New+infiltration&planetid=" + PlanetID + "&infplanetid=" + infPlanetID;

    GM_xmlhttpRequest({
        method: 'GET',
        url: myURL,
        headers: {
            'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
            'Accept': 'application/atom+xml,application/xml,text/xml',
        },
        onload: function(responseDetails) {
                if (responseDetails.responseText.match(/<b>Invalid order<\/td><\/tr><tr><td><BR><center><font color=#AAAA77 face=verdana,arial size=2>The target planet is blocking all infiltrations[\s\S]<BR><BR>/im)) {
                    // Successful match
                    infiltrationResult = 'Invalid Order';
                } else {
                    // Match attempt failed
                    infiltrationResult = 'Infiltration Successfully Created';
                }
        }
    });

내가 추가 할 때

Alert (InfiltrationResult);

지정된 직후, 나는 문자열을 올바르게 본다.

그러나 함수가 종료 된 후에는 동일한 경고를 시도하고 다음과 같습니다.

undefined

내가 잘못하고있는 아이디어가 있습니까?

도움이 되었습니까?

해결책

요청은 비동기 적으로 실행됩니다. 그게 함수는 AN을 취합니다 onload 콜백 기능. 그렇다면 동기식이라면 GM_xmlhttpRequest 일반 기능과 같은 응답 세부 사항을 간단히 반환합니다.

반환 요청을 기다리는 동안 전화 후 코드가 GM_xmlhttpRequest 계속 실행됩니다. 스크립트가 올바르게 식별합니다 infiltrationResult 요청이 아직 완료되지 않았기 때문에 정의되지 않았습니다.

요청이 다시 올 때 변수를 할당하는 것 이상을 수행 해야하는 경우 onload 콜백.

다른 팁

이 시도:

var infiltrationResult;

while(thisOption) {
    var trNode = document.createElement('tr');
    var tdNode = document.createElement('td');
    var hrefNode = document.createElement('a');

    infPlanetID = thisOption.getAttribute('value');

  var myURL = "http://www.hyperiums.com/servlet/Planetinf?securitylevel=90&newinfiltr=New+infiltration&planetid=" + PlanetID + "&infplanetid=" + infPlanetID;

    GM_xmlhttpRequest({
        method: 'GET',
        url: myURL,
        headers: {
            'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
            'Accept': 'application/atom+xml,application/xml,text/xml',
        },
        onload: function(responseDetails) {
                        if (responseDetails.responseText.match(/<b>Invalid order<\/td><\/tr><tr><td><BR><center><font color=#AAAA77 face=verdana,arial size=2>The target planet is blocking all infiltrations[\s\S]<BR><BR>/im)) {
                                // Successful match
                                infiltrationResult = 'Invalid Order';
                        } else {
                                // Match attempt failed
                                infiltrationResult = 'Infiltration Successfully Created';
                        }
                        sentback(infiltrationResult);//Sent it when it loads only
        }
    });

function sentback(x){
    alert(x);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top