Javascript无法控制方法

时间:2016-05-13 12:05:31

标签: javascript jquery

我在下面的代码中遇到的问题很少。任何人都可以帮忙吗?

  1. getStatusUsingAjax methid中的if(currentStatus!=“undefined”)块无效。
  2. 无法控制getStatusUsingAjax方法
  3.  $(document).ready(
       loadStatus()
    
     );
    
     function loadStatus(x) {
    
         $('.a-IRR-table tr').each(function(i) {
               var val = $(this).find("td").eq(0).text();
               link = $(this).find("td").eq(0).find("a").attr("href");
               linkTag = $(this).find("td").eq(0).find("a");
    
               `if ((val !== "-") && (val !== "")) {
              console.log("val is" + val);
              if (verifyrequestArray(val)) {
               console.log("inside second if");
    
              } else {
                console.log("inside else");
                sleep(1.5 * 1000);
                var updatedStatus2 = getStatusUsingAjax(val, link);
                console.log("UpdatedStatus2 is " + updatedStatus2);
                setTooltip(linkTag, updatedStatus2);
              }
    
    
    
            }
          });
    
        }
    
            function verifyrequestArray(id) {
    
          var newArray = requestArray.toString().split('-');
          console.log("NewArray is :" + newArray);
          for (i = 0; i < newArray.length; i++) {
            // I'm looking for the index i, when the condition is true
            if (newArray[i] === id) {
              console.log("request id found" + newArray[i]);
              break;
            } else {
              console.log("request id not found" + newArray[i]);
              return false;
            }
          }
        }
    
    
    
            function getStatusUsingAjax(requestValue, currentlink) {
    
          console.log("patch req: " + requestValue);
          console.log("Link is " + currentlink);
          var currentStatus;
          GM_xmlhttpRequest({
            method: "GET",
            url: currentlink,
            onload: function(response) {
    
              if ($(response.responseText).find("#P16_STATUS2").size() === 1) {
                currentStatus = $(response.responseText).find("#P16_STATUS2").text();
                console.log("Current Status from #P16_STATUS2 is :" + currentStatus);
                console.log("Final URL is " + response.finalUrl);
              }
    
    
            }
    
          });
          // console.log("Final URL is " +response.finalUrl);
    
          if (currentStatus != "undefined") {
    
            var pusharr = [requestValue + "-" + currentStatus];
            requestArray.push(pusharr);
            console.log("Updated Array is " + requestArray);
            return currentStatus;
          }
        }
    
           function setTooltip(currentTag, status2) {
            console.log("in settooltip" + currentTag);
            currentTag.attr("title", status2); //setting status a tooltip
        }

    错误出现的任何线索?

1 个答案:

答案 0 :(得分:0)

fn getStatusUsingAjax是异步的,您无法使用以下方式获取返回值:

var updatedStatus2 = getStatusUsingAjax(val, link);

你sholud创建回调:

function getStatusUsingAjax(val, link, callback){
//...
// do not use return currentStatus, call calback fn instead
callback(currentStatus)
}

并使用:

调用它
getStatusUsingAjax(val, link, function(updatedStatus2){ 
  console.log("UpdatedStatus2 is " + updatedStatus2);
  setTooltip(linkTag, updatedStatus2); 
});