我使用ImgLikeOpera和Squid Caching Proxy在拨号时管理我的带宽。但是,我无法将其设置为一次加载一个图像,所以我有一个好主意,就是编写一个脚本,在一个新页面中一次打开一个页面上的每个图像,然后关闭它们以便它们'将保存在我的缓存中。
脚本效果很好,添加了一个开始按钮,以便我可以控制它何时启动...但无法弄清楚如何制作一个会中断该过程的停止按钮。我尝试了很多东西,没有任何作用......
似乎当它在循环中时,它无法听到循环外发生的事情......
我觉得有一种非常简单的方法可以做到这一点,但我感到很沮丧。这不是应该是什么休息或回归?
以下是我的脚本的相关部分:
var box = document.createElement ('div');
box.id = 'mySelectBox';
document.body.appendChild (box);
box.innerHTML = 'click>';
var searchButton = document.createElement ('div');
searchButton.className = 'mySearchButton';
searchButton.textContent = 'Search and open';
box.insertBefore (searchButton, box.nextSibling);
var stopButton = document.createElement ('div');
stopButton.className = 'myStopButton';
stopButton.textContent = 'Stop';
box.insertBefore (stopButton, box.nextSibling);
var mytable = document.getElementById ('lair-sort-pets').getElementsByTagName ('img');
var linksToOpen = [];
var mywin2 = null;
function openpics () {
for (var J = 0, L = mytable.length; J < L; J++) {
linksToOpen.push (mytable[J].src); //-- Add URL to list
}
openLinksInSequence ();
};
function openLinksInSequence () {
if (mywin2) {
mywin2.close ();
mywin2 = null;
}
if (linksToOpen.length) {
var link = linksToOpen.shift ();
mywin2 = window.open (link, "my_win2");
mywin2.addEventListener ('load', openLinksInSequence, false);
}
}
searchButton.addEventListener ('click', openpics, true);
//stopButton.addEventListener ('click', , true);
如何让停止按钮实际停止加载链接?
答案 0 :(得分:1)
使用全局状态变量。像这样:
var okayToOpenLinks = true;
searchButton.addEventListener ('click', openpics);
stopButton.addEventListener ('click', stopLinkSequence);
function openpics () {
okayToOpenLinks = true;
if (linksToOpen.length === 0) {
for (var J = 0, L = mytable.length; J < L; J++) {
linksToOpen.push (mytable[J].src); //-- Add URL to list
}
}
openLinksInSequence ();
};
function stopLinkSequence () {
okayToOpenLinks = false;
}
function openLinksInSequence () {
if (mywin2) {
mywin2.close ();
mywin2 = null;
}
if (okayToOpenLinks && linksToOpen.length) {
var link = linksToOpen.shift ();
mywin2 = window.open (link, "my_win2");
mywin2.addEventListener ('load', openLinksInSequence, false);
}
}