3

我正在開發谷歌瀏覽器擴展程序,我有一個相當......奇怪的問題。 (我會盡量保持這個儘可能簡單)setInterval使用last之前的值嗎?

基本上,我允許用戶定製setInterval循環的間隔速度。 我這樣做的輸入框,當輸入數據時,在keyUp上設置localStorage值。然後localStorage值將通過Chrome的消息傳遞API傳遞給內容腳本,並且setInterval循環會自動將其選中。

options.html

<!DOCTYPE html> 
    <head> 
    </head> 

    <body> 
     <input type="text" id="customTime_Input" onKeyUp="setPref_customTime()" /> 

     <!--Call JavaScript functions after all elements are created--> 
     <script type="text/javascript" src="messagePassing.js"></script> 
     <script type="text/javascript" src="options.js"></script> 
    </body> 
</html> 

background.html

<!DOCTYPE html> 
    <head> 
     <script type="text/javascript" src="messagePassing.js"></script> 
     <script type="text/javascript" src="content.js"></script> 
    </head> 

    <body> 
    </body> 
</html> 

messagePassing.js

if(localStorage["scanTime"] == undefined || localStorage["scanTime"] == ""){ 
    localStorage["scanTime"] = 2000; 
} 

chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) { 
     if (request.method == "getScanTime"){ 
      sendResponse({data: localStorage["scanTime"]}); 
      console.log("scanTime LS, response: " + localStorage["scanTime"]); 
     } 
    } 
); 

個options.js

function setPref_customTime(){ 
    var inputBoxElement = document.getElementById("customTime_Input"); 

    localStorage["scanTime"] = inputBoxElement.value; 
    console.log("scanTime LS, setPref_customTime(): " + localStorage["scanTime"]); 
} 

content.js

window.load = passMessages(); 
function passMessages(){ 
    chrome.extension.sendRequest({method: "getScanTime"}, function(response) { 
     localStorage["scanTime"] = response.data; 
     console.log("scanTime LS, request: " + localStorage["scanTime"]); 
    }); 

    scanPage(); 
} 

function scanPage(){ 
    console.log("scanTime LS, scanPage: " + localStorage["scanTime"]); 

    setInterval(function match(){ 
     console.log("scanTime LS, match: " + localStorage["scanTime"]); 
    }, localStorage["scanTime"]); 
} 

,進入自定義值後控制檯返回這些日誌(它返回「2000」的所有日誌時,默認值是裝,它應該如此):

scanTime LS, scanPage: 2000 
scanTime LS, scanPage: 2000 
scanTime LS, request: 5000 
scanTime LS, request: 5000 
scanTime LS, scanPage: 2000 
scanTime LS, request: 5000 
scanTime LS, match: 5000 
scanTime LS, match: 5000 
scanTime LS, match: 5000 
scanTime LS, match: 5000 
[etc] 

它清楚地表明setInterval正在使用新的值,ye t頁面和日誌以舊值的速度刷新(並且沒有用戶錯誤。我多次使用了大量不同的值,並得到了相同的結果)。具有相同名稱的localStorage如何在幾行之間有不同的值?

更新:我剛剛注意到,控制檯報告除「scanTime LS,response:」之外的所有console.log()命令。這很重要嗎?

回答

0

你看到的問題是你有一個循環運行2000毫秒,然後開始一個新的循環與5000毫秒讓你兩個循環一次運行。當您使用clearInterval開始第二個時,您需要取消第一個。

這樣的事情應該工作。

function scanPage(){ 
    console.log("scanTime LS, scanPage: " + localStorage["scanTime"]); 

    if (localStorage["intervalID"]) { 
     clearInterval(localStorage["intervalID"]); 
    } 

    localStorage["intervalID"] = setInterval(function match(){ 
     console.log("scanTime LS, match: " + localStorage["scanTime"]); 
    }, localStorage["scanTime"]); 
} 
+0

是的,我認爲奇怪的是多個日誌被報告。我現在會嘗試並回復你。 :) – mythofechelon 2012-04-13 21:54:53

+0

沒有。插入你的代碼,它仍然具有完全相同的效果。 :/ – mythofechelon 2012-04-13 22:01:06

+0

您可能必須將intervalID轉換回int。 – abraham 2012-04-14 00:21:52