停止命令正在从 Web 浏览器控制台执行
Stop command is excuting from web browser console
我想在 chrome 控制台中执行一个循环,但我想在 运行 时停止它(不关闭网络浏览器)。那么该怎么做。非常感谢您的帮助。
这是我的脚本,我想停止它:
for(var i=0;i<20;i++) {
(function (i) {
setTimeout(function () {
{
scrollBy(1500, 999999);
}
}, 8000 * i);
}(i));
};
setTimeout(function () {
alert('Finish--------------!');
}, 8000 * (i));
也许试试另一个函数结构:
例如
//CODE WILL STOP WHEN PAGE IS NEARLY END
var i=0;
function someFunc() {
setTimeout(function () {
{
scrollBy(0, 200);
}
}, 8000 * i);
i++;
if($(window).height()-window.pageYOffset < 200) i=20;
if(i<20) someFunc();
}
someFunc();
如果我错了请纠正我,但我认为您是在尝试清除超时对象而不是停止循环。
尝试这样的事情:
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() 绑定到某种事件。
我想在 chrome 控制台中执行一个循环,但我想在 运行 时停止它(不关闭网络浏览器)。那么该怎么做。非常感谢您的帮助。
这是我的脚本,我想停止它:
for(var i=0;i<20;i++) {
(function (i) {
setTimeout(function () {
{
scrollBy(1500, 999999);
}
}, 8000 * i);
}(i));
};
setTimeout(function () {
alert('Finish--------------!');
}, 8000 * (i));
也许试试另一个函数结构: 例如
//CODE WILL STOP WHEN PAGE IS NEARLY END
var i=0;
function someFunc() {
setTimeout(function () {
{
scrollBy(0, 200);
}
}, 8000 * i);
i++;
if($(window).height()-window.pageYOffset < 200) i=20;
if(i<20) someFunc();
}
someFunc();
如果我错了请纠正我,但我认为您是在尝试清除超时对象而不是停止循环。
尝试这样的事情:
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() 绑定到某种事件。