Chrome 扩展 - 初始停止后无法重新开始滚动间隔
Chrome extension - not able to re-start scrolling interval after initial stop
我的扩展程序需要平滑地滚动到页面底部,这很完美。当 request.doScroll
更改为 false
时,滚动停止正常。但是,如果我尝试重新启用滚动,它不会重新启动。
任何想法为什么以及如何实现 stop\start 等功能?
//starts fine for the first time and does not start for the second time
if(request.doScroll){
chrome.tabs.executeScript(sender.tab.id, {
code:`
let x = 1; //y-axis pixel displacement
let y = 1; //delay in milliseconds
const t = setInterval(()=> {
window.scroll(0, x);
x = x + 5; //to increase speed increase increment interval
if($(window).scrollTop() + $(window).height() > $(document).height() - 100) {
console.log("clearing scrolling interval");
clearInterval(t);
}
}, y);
`
});
}else{
chrome.tabs.executeScript(sender.tab.id,
{
//this stops the scrolling
code: "clearInterval(t);"
}
);
}
尝试用这个改变“if”块:
code:`
var t;
var x; //y-axis pixel displacement
(_ => {
let y = 1; //delay in milliseconds
t = setInterval(()=> {
window.scroll(0, x);
x = (x||1) + 5; //to increase speed increase increment interval
if($(window).scrollTop() + $(window).height() > $(document).height() - 100) {
console.log("clearing scrolling interval");
clearInterval(t);
x = 1 /* only if you want...*/
}
}, y);
})()`
我的扩展程序需要平滑地滚动到页面底部,这很完美。当 request.doScroll
更改为 false
时,滚动停止正常。但是,如果我尝试重新启用滚动,它不会重新启动。
任何想法为什么以及如何实现 stop\start 等功能?
//starts fine for the first time and does not start for the second time
if(request.doScroll){
chrome.tabs.executeScript(sender.tab.id, {
code:`
let x = 1; //y-axis pixel displacement
let y = 1; //delay in milliseconds
const t = setInterval(()=> {
window.scroll(0, x);
x = x + 5; //to increase speed increase increment interval
if($(window).scrollTop() + $(window).height() > $(document).height() - 100) {
console.log("clearing scrolling interval");
clearInterval(t);
}
}, y);
`
});
}else{
chrome.tabs.executeScript(sender.tab.id,
{
//this stops the scrolling
code: "clearInterval(t);"
}
);
}
尝试用这个改变“if”块:
code:`
var t;
var x; //y-axis pixel displacement
(_ => {
let y = 1; //delay in milliseconds
t = setInterval(()=> {
window.scroll(0, x);
x = (x||1) + 5; //to increase speed increase increment interval
if($(window).scrollTop() + $(window).height() > $(document).height() - 100) {
console.log("clearing scrolling interval");
clearInterval(t);
x = 1 /* only if you want...*/
}
}, y);
})()`