Javascript递归:突破

时间:2014-04-11 17:24:53

标签: javascript recursion google-chrome-extension

我很难摆脱这种递归功能。该脚本是chrome扩展的后台脚本,并将命令发送到页面上的内容脚本。对于jsonArray中的每个元素,脚本需要:

  1. 查询页面并等待页面刷新
  2. 抓住特定标题
  3. 一旦完成整个阵列,就应该停止。

    这是我得到的:

    function goGetEm(counter, path){
    
        key = jsonArray[counter];
    
        console.log("This is round "+counter+" and I am going to "+path);
    
        if(counter>=jsonArray.length){
            console.log("finishing");
            return;
        }
    
        if(path=="query"){
            console.log("querying "+jsonArray[counter]);
            message.sendMessage(tabID, {
                "submitquery": {
                    "query": jsonArray[counter],
                    "searchFieldID": searchFieldID,
                    "submitID": submitID
                }
            }, function(response){
                chrome.tabs.onUpdated.addListener(function(tabID , info) {
                    if (info.status == "complete") {
                        console.log("query complete");
                        goGetEm(counter, "grab");
                        return;
                    }
                });
            });
        }
        else if(path=="grab"){
            console.log("grabbing");
            message.sendMessage(tabID, {
                "grabdata": {
                    "targetID": targetID
                }
            }, function(response){
                console.log("grab complete");
                saveData(key, response);
                goGetEm(counter+1, "query");
                return;
            });
        }
        return;
    }
    
    goGetEm(0, "query");
    

    该功能确实成功提交查询并获取数据。但是我相信我已经失去了所有被调用的不同循环的轨道而没有正确地结束它们(我认为这是我所有的"返回"会做什么)..

    这是输出(好吧,其中一些)

    This is round 0 and I am going to query bg.js:58
    querying element0 bg.js:66
    query complete bg.js:76
    This is round 0 and I am going to grab bg.js:58
    grabbing bg.js:84
    grab complete bg.js:90
    This is round 1 and I am going to query bg.js:58
    querying element1 bg.js:66
    query complete bg.js:76
    This is round 0 and I am going to grab bg.js:58
    grabbing bg.js:84
    query complete bg.js:76
    This is round 1 and I am going to grab bg.js:58
    grabbing bg.js:84
    grab complete bg.js:90
    This is round 1 and I am going to query bg.js:58
    querying element1 bg.js:66
    grab complete bg.js:90
    This is round 2 and I am going to query bg.js:58
    finishing bg.js:60
    Object
     bg.js:48
    query complete bg.js:76
    This is round 0 and I am going to grab bg.js:58
    grabbing bg.js:84
    query complete bg.js:76
    This is round 1 and I am going to grab bg.js:58
    grabbing bg.js:84
    query complete bg.js:76
    This is round 1 and I am going to grab bg.js:58
    grabbing bg.js:84
    grab complete bg.js:90
    This is round 1 and I am going to query bg.js:58
    querying element1 bg.js:66
    grab complete bg.js:90
    This is round 2 and I am going to query bg.js:58
    finishing bg.js:60
    Object
     bg.js:48
    grab complete bg.js:90
    This is round 2 and I am going to query bg.js:58
    finishing bg.js:60
    Object
     bg.js:48
    query complete bg.js:76
    This is round 0 and I am going to grab bg.js:58
    grabbing bg.js:84
    query complete bg.js:76
    This is round 1 and I am going to grab bg.js:58
    grabbing bg.js:84
    query complete bg.js:76
    This is round 1 and I am going to grab bg.js:58
    grabbing bg.js:84
    query complete bg.js:76
    This is round 1 and I am going to grab bg.js:58
    grabbing bg.js:84
    grab complete bg.js:90
    This is round 1 and I am going to query bg.js:58
    querying element1 bg.js:66
    grab complete bg.js:90
    

    然后......

1 个答案:

答案 0 :(得分:0)

我认为,混合你的是你有异步IO在那里。递归是关于调用当前函数并维护一个调用堆栈,但由于你正在处理回调,这并不像你想象的那样真正发生。此return从侦听器返回,而不是从外部容器返回。

chrome.tabs.onUpdated.addListener(function(tabID , info) {
    if (info.status == "complete") {
        console.log("query complete");
        goGetEm(counter, "grab");
        return;
    }
});

我要做的就是重写它,以便它是迭代的。我有一个带回调方法的对象,对象内部跟踪状态的方式与堆栈相同。

var worker = {
    queue:[],
    onUpdatedListener: function(tabID , info) {
        ...
    },
    sendMessageComplete: function(response) { ... },
    doWork: function(...) { ... },
    init: function(queue) {
        this.queue = queue;
    }
}

worker.init(jsonArray);
worker.doWork();