使用原生js做一个简单的定时访问页面

2020年3月的某一个周末,我在无聊之下想通过js实现刷浏览量的功能,它的优点是不需要引入任何东西,只需要建一个html页面,将我们的js代码加进去就可以执行。(记得允许你的浏览器打开其他页面)

开始思路,通过js的for循环一直访问文章地址,链接方式使用window.open("地址")的方式

$(function (){
        for (var i=0; i<100; i++) {
            window.open("https://www.baidu.com/");
        }
    })

可以看到这个是循环open了100次百度的页面,但是没有关闭,大量的标签在浏览器上让浏览器非常卡顿,于是我们要加入关闭标签的功能。

location.close();

这个是关闭标签页的功能,但是测试后我们是关闭了我们js的页面,我们实际要关闭的是open的页面,再修改一下,在我们使用open的时候用var定义接受一下对象以便于关闭打开的标签页

$(function (){
        for (var i=0; i<100; i++) {
            var ww = window.open("https://www.baidu.com/");
            ww.close();
            window.open("https://www.baidu.com/");
        }
    })

这样就只会关闭我们打开的页面,但是我们要实现自动化就要用定时执行了

window.setInterval(a,5000); // a是方法名 5000是毫秒  意思就是每隔5000毫秒调用a方法

function a (){
   var ww = window.open("https://www.baidu.com/");
   ww.close();
   window.open("https://www.baidu.com/");
}

但是实际情况肯定并不会只打开一个页面,于是我们再优化一下,将要打开的页面定义在数组里,这样每次定时执行会先关闭之前的页面,再执行我们定义好的数组地址。

var ad = ["https://blog.csdn.net/AnNanDu/article/details/104946524","https://blog.csdn.net/AnNanDu/article/details/104796290","https://blog.csdn.net/AnNanDu/article/details/104795235",
        "https://blog.csdn.net/AnNanDu/article/details/104718395","https://blog.csdn.net/AnNanDu/article/details/104697868","https://blog.csdn.net/AnNanDu/article/details/104559232","https://blog.csdn.net/AnNanDu/article/details/104551022",
        "https://blog.csdn.net/AnNanDu/article/details/104532424","https://blog.csdn.net/AnNanDu/article/details/104521307","https://blog.csdn.net/AnNanDu/article/details/104311229","https://blog.csdn.net/AnNanDu/article/details/104248809",
        "https://blog.csdn.net/AnNanDu/article/details/104248809","https://blog.csdn.net/AnNanDu/article/details/103888862","https://blog.csdn.net/AnNanDu/article/details/103817183","https://blog.csdn.net/AnNanDu/article/details/103800005",
        "https://blog.csdn.net/AnNanDu/article/details/103765749"
    ];

window.setInterval(a,5000); // a是方法名 5000是毫秒  意思就是每隔5000毫秒调用a方法

function a (){
   for (var i=0; i<ad.length; i++) {
            var ww = window.open(ad[i]);
            ww.close();
            if (ww==null){
                window.open(ad[i]);
            }
   }
}

但是如果要每次要打开100个地址还是对浏览器有影响,所以我们多定义几个定时js来让他们交错开

var ad = ["https://blog.csdn.net/AnNanDu/article/details/104946524","https://blog.csdn.net/AnNanDu/article/details/104796290","https://blog.csdn.net/AnNanDu/article/details/104795235",
        "https://blog.csdn.net/AnNanDu/article/details/104718395","https://blog.csdn.net/AnNanDu/article/details/104697868","https://blog.csdn.net/AnNanDu/article/details/104559232","https://blog.csdn.net/AnNanDu/article/details/104551022",
        "https://blog.csdn.net/AnNanDu/article/details/104532424","https://blog.csdn.net/AnNanDu/article/details/104521307","https://blog.csdn.net/AnNanDu/article/details/104311229","https://blog.csdn.net/AnNanDu/article/details/104248809",
        "https://blog.csdn.net/AnNanDu/article/details/104248809","https://blog.csdn.net/AnNanDu/article/details/103888862","https://blog.csdn.net/AnNanDu/article/details/103817183","https://blog.csdn.net/AnNanDu/article/details/103800005",
        "https://blog.csdn.net/AnNanDu/article/details/103765749"
    ];
    var bd = ["https://blog.csdn.net/AnNanDu/article/details/103732422","https://blog.csdn.net/AnNanDu/article/details/103729332","https://blog.csdn.net/AnNanDu/article/details/103528623",
        "https://blog.csdn.net/AnNanDu/article/details/103406640","https://blog.csdn.net/AnNanDu/article/details/103391666","https://blog.csdn.net/AnNanDu/article/details/103387220","https://blog.csdn.net/AnNanDu/article/details/103324327",
        "https://blog.csdn.net/AnNanDu/article/details/103270410","https://blog.csdn.net/AnNanDu/article/details/103262381","https://blog.csdn.net/AnNanDu/article/details/103259328","https://blog.csdn.net/AnNanDu/article/details/103251069",
        "https://blog.csdn.net/AnNanDu/article/details/103250990","https://blog.csdn.net/AnNanDu/article/details/103214815","https://blog.csdn.net/AnNanDu/article/details/103185475","https://blog.csdn.net/AnNanDu/article/details/103179238",
        "https://blog.csdn.net/AnNanDu/article/details/102719110"
    ];
    window.setInterval(a,5000);
    window.setInterval(b,6000);

    function a (){
        for (var i=0; i<ad.length; i++) {
            var ww = window.open(ad[i]);
            ww.close();
            if (ww==null){
                window.open(ad[i]);
            }
        }
    }

    function b(){
        for (var i=0; i<bd.length; i++) {
            var ww = window.open(bd[i]);
            ww.close();
            if (ww==null){
                window.open(bd[i]);
            }
        }
    }

可以看到我们将a、b两个方法的定时时间差了1s,所以在b执行的时候a已经关闭了他的所有页面,避免了同一时间打开过多页面,如果还要同时打开更多的页面,继续加定时任务和数组,他们直接的执行时间最好岔开2秒钟,下面为完整代码,直接复制粘贴打开页面就可以执行。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
Hello World
</body>
<script>



    var ad = ["https://blog.csdn.net/AnNanDu/article/details/104946524","https://blog.csdn.net/AnNanDu/article/details/104796290","https://blog.csdn.net/AnNanDu/article/details/104795235",
        "https://blog.csdn.net/AnNanDu/article/details/104718395","https://blog.csdn.net/AnNanDu/article/details/104697868","https://blog.csdn.net/AnNanDu/article/details/104559232","https://blog.csdn.net/AnNanDu/article/details/104551022",
        "https://blog.csdn.net/AnNanDu/article/details/104532424","https://blog.csdn.net/AnNanDu/article/details/104521307","https://blog.csdn.net/AnNanDu/article/details/104311229","https://blog.csdn.net/AnNanDu/article/details/104248809",
        "https://blog.csdn.net/AnNanDu/article/details/104248809","https://blog.csdn.net/AnNanDu/article/details/103888862","https://blog.csdn.net/AnNanDu/article/details/103817183","https://blog.csdn.net/AnNanDu/article/details/103800005",
        "https://blog.csdn.net/AnNanDu/article/details/103765749"
    ];
    var bd = ["https://blog.csdn.net/AnNanDu/article/details/103732422","https://blog.csdn.net/AnNanDu/article/details/103729332","https://blog.csdn.net/AnNanDu/article/details/103528623",
        "https://blog.csdn.net/AnNanDu/article/details/103406640","https://blog.csdn.net/AnNanDu/article/details/103391666","https://blog.csdn.net/AnNanDu/article/details/103387220","https://blog.csdn.net/AnNanDu/article/details/103324327",
        "https://blog.csdn.net/AnNanDu/article/details/103270410","https://blog.csdn.net/AnNanDu/article/details/103262381","https://blog.csdn.net/AnNanDu/article/details/103259328","https://blog.csdn.net/AnNanDu/article/details/103251069",
        "https://blog.csdn.net/AnNanDu/article/details/103250990","https://blog.csdn.net/AnNanDu/article/details/103214815","https://blog.csdn.net/AnNanDu/article/details/103185475","https://blog.csdn.net/AnNanDu/article/details/103179238",
        "https://blog.csdn.net/AnNanDu/article/details/102719110"
    ];
    window.setInterval(a,5000);
    window.setInterval(b,6000);

    function a (){
        for (var i=0; i<ad.length; i++) {
            var ww = window.open(ad[i]);
            ww.close();
            if (ww==null){
                window.open(ad[i]);
            }
        }
    }

    function b(){
        for (var i=0; i<bd.length; i++) {
            var ww = window.open(bd[i]);
            ww.close();
            if (ww==null){
                window.open(bd[i]);
            }
        }
    }
</script>
</html>

 

posted @ 2020-03-23 14:00  余生大大  阅读(67)  评论(0编辑  收藏  举报