gpt4 book ai didi

javascript - 正在从 Web 浏览器控制台执行停止命令

转载 作者:行者123 更新时间:2023-11-28 07:25:29 27 4
gpt4 key购买 nike

我想在 chrome 控制台中执行一个循环,但我想在它运行时停止它(不关闭网络浏览器)。那么如何做到这一点。非常感谢您的帮助。

这是我的脚本,我想停止这个:

for(var i=0;i<20;i++) {
(function (i) {
setTimeout(function () {
{
scrollBy(1500, 999999);
}
}, 8000 * i);
}(i));
};
setTimeout(function () {
alert('Finish--------------!');
}, 8000 * (i));

最佳答案

如果我错了,请纠正我,但我认为您正在尝试清除超时对象而不是停止循环。

尝试这样的事情:

var obj = [];

for(var i=0;i<20;i++) {
(function (i) {
obj.push(setTimeout(function () {
{
scrollBy(1500, 999999);
}
}, 8000 * i));
}(i));
}

obj.push(setTimeout(function () {
alert('Finish--------------!');
}, 8000 * (i)));

// when this function is called it will loop over the timeout objects
// you created in the above loop and clear them
function clearTO() {
var i;
for (i = 0; i < obj.length; i += 1) {
clearTimeout(obj[i]);
}
}

// if typing 'yes' in the prompt gives you the behavior
// you're looking for replace this timeout function with something more dynamic that fits your needs


var cnt = 0;
function stop() {
if (prompt("type yes to stop") === "yes") {
clearTO();
} else if(cnt < i){
cnt += 1;
setTimeout(stop, 8010);
};
}


stop();

显然,您必须将clearTO() 绑定(bind)到某种类型的事件。

关于javascript - 正在从 Web 浏览器控制台执行停止命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29719341/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com