函数递归时如何返回父函数Javascript

时间:2016-05-22 07:28:34

标签: javascript jquery ajax recursion wikipedia

我已经完成了FreeCodeCamp并且给了自己从Wikipedia API获取图像的任务。我很近,但我只是遇到了这个递归函数的问题。

我在使用ajax请求时遇到了一些麻烦。我希望整个成功函数在obj === label时返回。但是,它只返回findObjByLabel()的一个实例。

如果找到标签,我该怎样做才能使成功功能完全恢复?

var wikiUrl = "https://en.wikipedia.org/w/api.php?action=query&format=json&titles=India&prop=pageimages&pithumbsize=300&callback=?";


// this retrieves info about the wikiUrlImg
$.ajax( {
    url: wikiUrl,
     data: {
    format: 'json'
     },
    dataType: 'json',
    type: 'GET',
    headers: { 'Api-User-Agent': 'Example/1.0' },
    success: function(data) {          
        console.log("wiki api success");            
        var findLabel = findObjByLabel(data,"India",1);

        function findObjByLabel(obj, label, iterrations){
            var itterationLimit = "9";              
            if (iterrations < itterationLimit){
                    for(var i in obj){              
                        if(obj === label){ 
                            console.log(">>>>>>>>>>>>>>>>>>>>>>> !!!its the label!!! <<<<<<<<<<<<<<<<<<<<<<<<");                
        // ****************I want the success function to return here! ****************
                            return "something";
                        }else{
                            console.log(">>>>>>>>>>>>>>>>>>>>>>>its not the label<<<<<<<<<<<<<<<<<<<<<<<<");
                                console.log("i= "  + i);
                                if(obj.hasOwnProperty(i)){

                                    iterrations+=1;
                                    console.log("obj[i] : " + obj[i]);
                                    var foundLabel = findObjByLabel(obj[i], label, iterrations);

                                }       

                        }

                    }
            }

        }//end of findObjByLabel function         
},  //end of success

error: function(){
        console.log("failure of getWiki api");
    }

}); 

1 个答案:

答案 0 :(得分:0)

obj[i]条件下替换obj if,在break语句中使用if,在return之外放置for语句} loop

var wikiUrl = "https://en.wikipedia.org/w/api.php?action=query&format=json&titles=India&prop=pageimages&pithumbsize=300&callback=?";

// this retrieves info about the wikiUrlImg
$.ajax({
  url: wikiUrl,
  data: {
    format: 'json'
  },
  dataType: 'json',
  type: 'GET',
  headers: {
    'Api-User-Agent': 'Example/1.0'
  },
  success: function(data) {
    console.log("wiki api success");
    var findLabel = findObjByLabel(data, "India", 1);

    function findObjByLabel(obj, label, iterrations) {
        var itterationLimit = "9";
        if (iterrations < itterationLimit) {
          for (var i in obj) {
            if (obj[i] === label) {
              console.log(">>>>>>>>>>>>>>>>>>>>>>> !!!its the label!!! <<<<<<<<<<<<<<<<<<<<<<<<");
              // ****************I want the success function to return here! ****************
              break; // break `for` loop
            } else {
              console.log(">>>>>>>>>>>>>>>>>>>>>>>its not the label<<<<<<<<<<<<<<<<<<<<<<<<");
              console.log("i= " + i);
              if (obj.hasOwnProperty(i)) {

                iterrations += 1;
                console.log("obj[i] : " + obj[i]);
                var foundLabel = findObjByLabel(obj[i], label, iterrations);

              }
            }
          }
        }
        return "something"; // return `"something"`
      } //end of findObjByLabel function  
    console.log(findLabel); // "something"
  }, //end of success

  error: function() {
    console.log("failure of getWiki api");
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>