【问题标题】:JS Browser with pause function, help pleaseJS浏览器有暂停功能,请帮忙
【发布时间】:2015-06-18 18:53:41
【问题描述】:

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

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 语句...

【问题讨论】:

  • 那么...你真的想在 in JavaScript 中编写一个浏览器,而不是在另一个浏览器中
  • 我想要一个浏览器,不知道是什么语言,我写的程序是用 JavaScript 写的,但你有这个想法。我不想使用 Chrome 或 Mozilla,我想要一个程序来显示程序中数组的链接。如果这有意义...

标签: javascript function browser


【解决方案1】:

为您的超时时间添加一个变量,而不是使用值 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);
}

【讨论】:

  • 这会每 3 个链接暂停一次吗?
猜你喜欢
  • 2011-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
  • 2020-12-15
  • 1970-01-01
  • 2017-01-08
  • 1970-01-01
相关资源
最近更新 更多