簡體   English   中英

Firefox XPCOM setTimeout問題

[英]Firefox XPCOM setTimeout problem

我正在編寫一個簡單的firefox擴展,它會爬網一堆URL並提取某些字段(所有被爬網的URL都將加載到用戶選項卡中)。

我面臨的問題是實際訪問URL並加載頁面的部分。 我希望每個頁面以固定的計時器周期加載。 例如,每個站點每5秒訪問一次。

我嘗試了http://groups.google.com/group/mozilla.dev.extensions/browse_thread/thread/de47c3949542b759此處列出的兩種方法,但均無濟於事。 同時使用Components.classes [“ @ mozilla.org/appshell/appShellService;1”]和nsITimer。 while循環立即執行,並且頁面稍后加載(大約5秒鍾后快速連續加載)

 function startCrawl()
    {
        while(urlq.length>0)
        {
            var currentUrl = urlq.shift();
            urlhash[currentUrl]=1;

            if(currentUrl!=undefined)
            {
                setTimeout(gotoURL,5000,currentUrl);
            }

        }
            start=0;
            alert('crawl stopped');

            for(var k in foundData)
            {
                alert('found: ' + k);
            }           

    }

    function gotoURL(gUrl)
    {
        mainWindow.content.wrappedJSObject.location=gUrl;
        extractContent();

    }

如何實現可每5秒正確調用gotoURL的計時器功能? 謝謝!

好吧, setTimeout是異步執行的。 循環不會等到調用該函數。 您必須更改策略(如果我理解正確的話)。

例如,您可以在提取信息后觸發下一個setTimeout

function startCrawl() {
    function next() {
        var currentUrl = urlq.shift();
        if(currentUrl) {
            setTimeout(gotoURL,5000,currentUrl, next);
        }
    }
    next();    
}

function gotoURL(gUrl, next) {
    mainWindow.content.wrappedJSObject.location=gUrl;
    extractContent();
    next();
}

是的,最好使用nsITimer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM