所以我正在嘗試創建兩個按鈕。一個是間隔重繪 站點,第二個應該在按下后停止它,但是停止按鈕不起作用。第二件事是,如果我按下開始按鈕,它不會像我想要的那樣保存在本地存盤中。你們能幫我解決這個問題嗎?
window.onload = function () {
if (localStorage.getItem("refresh")) {
startref()
}
};
function startref() {
let intervalId = setInterval(function () {
chrome.tabs.query({ active: true, currentWindow: true }, function (arrayOfTabs) {
var code = 'window.location.reload();';
chrome.tabs.executeScript(arrayOfTabs[0].id, { code: code });
});
}, 1000);
localStorage.setItem('refresh');
}
function stop() {
clearInterval(intervalId);
}
document.addEventListener('DOMContentLoaded', function () {
document.getElementById("startbtn").addEventListener('click', startref);
});
document.addEventListener('DOMContentLoaded', function () {
document.getElementById("stopbtn").addEventListener('click', stop);
});
uj5u.com熱心網友回復:
localStorage.setItem函式有兩個引數。一個是鍵,另一個是鍵的值。
更改此行
localStorage.setItem('refresh');
進入這個
localStorage.setItem('refresh',intervalId);
當您清除間隔時,首先將intervalId存盤在 localStorage 中,然后呼叫clearInterval.
function stop() {
const intervalId = localStorage.getItem("refresh");
clearInterval(intervalId);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/479694.html
標籤:javascript html
