具有暂停功能的JS浏览器,请帮忙

JS Browser with pause function, help please

本文关键字:浏览器 JS 暂停 功能      更新时间:2023-09-26

我用js编写了这个程序,它通过URL列表,在每个页面上停留几秒钟,关闭当前窗口,然后打开下一行。一切正常,现在我需要它每 5 个链接停止/暂停一次。这个项目的第二部分是创建我自己的浏览器,它像程序一样打开,并且会有三个按钮(开始,继续,停止,也许也暂停)。我希望开始按钮显然启动浏览页面的功能,继续将在第五个链接上暂停时,我希望弹出消息说"唤醒"并可以选择仅单击"确定"。然后,您必须单击继续才能使该功能继续。Stop 将停止该函数,无论它到达列表中的哪个位置。我希望这些链接显示在我的浏览器中,而不是在谷歌浏览器或任何其他浏览器中。我应该使用什么程序来设计浏览器?这是当前程序的代码:

var urlList = ['www.youtube.com',
            'www.google.com', 
            'www.bing.com',
            'www.yahoo.com', 
            'www.facebook,com',
            'www.windows.com', 
            'www.opera.com',];
var wnd;
var curIndex = 0; // a var to hold the current index of the current url
function openWindow(){
    wnd = window.open(urlList[curIndex], '', '');
    if (curIndex % 5 == 0) {
                           }
    setTimeout(function () {
         wnd.close(); //close current window
         curIndex++; //increment the index
         if(curIndex < urlList.length) openWindow(); //open the next window if the array isn't at the end
}, 4000);
}
openWindow();

帮我完成 if 语句...

为超时期限添加一个变量,而不是使用值 4000。请注意,它必须具有全局范围。我在这里添加了一个名为 delay 的变量:

var wnd;
var curIndex = 0; // a var to hold the current index of the current url
var delay;

然后,在 openWindow() 函数中使用新变量,在您希望暂停时在 if 语句中将其值设置为更长的时间段。

我在这里使用了三元运算符而不是if语句,但您也可以使用 if 语句:

function openWindow(){
    wnd = window.open('http://' + urlList[curIndex], '', '');
    // pause for 30 seconds instead of 4 if the condition is met
    delay = (curIndex > 0 && curIndex % 3 == 0 ? 30000 : 4000)
    setTimeout(function () {
         wnd.close(); //close current window
         curIndex++; //increment the index
         if(curIndex < urlList.length) openWindow(); //open the next window if the array isn't at the end
}, delay);
}