我的程序在每次迭代操作部分url后打开2-7个网页(在url中增加日期值)。我希望我的程序在打开下一个url之前暂停。打开URL 1 ->等待1.5秒->打开URL 2...etc
我的javascript函数如下所示:
function submitClicked(){
save current date in URL as variable
loop(4 times){
window.open(urlString); //open the initial URL
var newDate = getNextDay(date);
urlString.replace(date, newDate); (ex: if 2016-12-31 then replace it in URL with with 2017-01-01)
**wait 1.5 seconds**
}
function getNextDay(date){
...
return result (String value)
}
基本上,我想让它在循环的每一次迭代结束时暂停1.5秒。我用Java做了同样的程序,只使用了Thread.sleep(1500);
发布于 2016-04-26 22:42:12
您永远不要试图阻止JavaScript中的线程执行,因为这会导致浏览器感到口吃,并且通常会为用户提供非常糟糕的体验。您可以使用setInterval
重构以防止这种情况发生。
arr = ['http://site1', 'http://site2', 'http://site3'];
timer = null;
function instantiateTimer(){
timer = setInterval(openPage, 1000); // 1 second
}
function openPage(){
if(arr.length > 0){
page = arr.pop();
window.open(page) // some browsers may block this as a pop-up!
}else{
clearInterval(timer);
}
}
https://stackoverflow.com/questions/36876976
复制相似问题