[英]loop between links . is opening simultaneous multiple pages and doesn't stop
I know that is a stupid question,but i can't figure it out.(I'm learning, so please be kind ) 我知道这是一个愚蠢的问题,但我无法弄清楚。(我正在学习,所以请客气)
It's opening simultaneous multiple pages and doesn't stop when var page2 is empty, still continue opening blank page. 它同时打开多个页面,并且当var page2为空时不会停止,仍然继续打开空白页面。
I think the problem whit blank page is if(page.length===0) loop();
我认为空白页面的问题是if(page.length===0) loop();
, but if i use if(page.length===0) return;
,但是如果我使用if(page.length===0) return;
how can i call the loop function when is done? 完成后如何调用循环功能?
I want that after is finish to push the links in var page2, to start loop between that links one by one 我希望在完成后将变量page2中的链接推入,开始在链接之间逐一循环
var page = [ "link1" , "link2" , "link3"];
var page2 = [];
function pushToPage2Arr() {
if(page.length===0) loop();
var url = page.shift();
var openWindow = window.open(url, "_blank","height=500","width=500");
setTimeout(function () {
var cat = openWindow.document.getElementsByClassName("list-id");
for (var i = 0; i < cat.length ; i++) {
var id = openWindow.document.getElementsByClassName("list-id")[i].children[0].children[1].textContent;
var cbn = openWindow.document.getElementsByClassName("list-cbn")[i].children[3].textContent;
page2.push("http://blabla.com?id="+id+"&cbn="+cbn);
}
openWindow.close();
pushToPage2Arr();
}, 2000);
}
pushToPage2Arr();
function loop() {
if(page2.length===0) return;
var url2 = page2.shift();
var openWindow2 = window.open(url2, "_blank","height=500","width=500");
setTimeout(function () {
//do someting
openWindow2.close();
loop();
}, 3000);
}
So you want to exhaust page1, populating page2 in the process, and then you want to exhaust page2. 因此,您要用尽page1,在此过程中填充page2,然后要用尽page2。
You could just correct the error you're already focused on -- that control returns to pushToPage2Arr from loop: 您可以更正您已经关注的错误-该控件从循环返回到pushToPage2Arr:
if(page.length===0) loop();
var url = page.shift();
...
Should be: 应该:
if(page.length===0) { loop(); return; }
var url = page.shift();
But what you have here is really a 'delayed for-each'. 但是,您在这里拥有的确实是一个“延迟每个”。 So why not just write that? 那么,为什么不写呢?
Here's a single page script that sets a span to each number in a list, and then sets it to 'done' after the loop ends. 这是一个单页脚本,该脚本将跨度设置为列表中的每个数字,然后在循环结束后将其设置为“完成”。
<!doctype html>
<p>hello: <span id=text></span></p>
<script>
;(function() {
function delayedForEach(list, fun, timeout, andthen) {
function process() {
var x = list.shift()
if (x === undefined) {
andthen()
} else {
fun(x)
setTimeout(process, timeout)
}
}
process()
}
var stuff = [1,2,3,4],
text = document.getElementById('text')
delayedForEach(stuff, function(x) { text.textContent = String(x) }, 500,
function() { text.textContent = 'done' })
})()
</script>
You could also zip both lists with their corresponding functions, and end with with a dummy object and corresponding finalizer: 您还可以压缩两个列表及其相应的功能,并以虚拟对象和相应的终结器结尾:
function delayedDo(list, delay) {
function process() {
var x = list.shift()
if (x !== undefined) {
x[1](x[0])
setTimeout(process, delay)
}
}
process()
}
var text = document.getElementById('text'),
num = function(x) { text.textContent = '(number: ' + x + ')' },
str = function(x) { text.textContent = '(string: ' + x + ')' },
done = function() { text.textContent = 'done!' }
joblist =
[ [ 1, num ],
[ 2, num ],
[ 3, num ],
[ 'hello', str ],
[ 'world', str ],
[ null, done ] ]
delayedDo(joblist, 1000)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.