js 数组 splice 函数 多线程

<script type="text/javascript">

var arr = new Array(6)
arr[0] = "00"
arr[1] = "11"
arr[2] = "22"
arr[3] = "33"
arr[4] = "44"
arr[5] = "55"

document.write(arr + "<br />")
//arr.splice(2,3,"44")  //删除 22(索引 -2) 和 33(索引 -3)
//arr.splice(2,3)  //删除 22(索引 -2) 和 33(索引 -3)和 44(索引 -4)
//arr.splice(2,3,44,66)  //删除 22(索引 -2) 和 33(索引 -3)和 44(索引 -4)   增加 44 和 66
//arr.splice(0,1); //从索引0开始 删除1个
arr.splice(0,2); //从索引0开始 删除2个
document.write(arr)

</script>

 

<script type="text/javascript">

        var thread = function () {
            var nowTime = 0, //线程已经执行了多久
                maxTime = 15;//线程最多执行多久
            var threadArr = [];//数组模拟线程队列

            this.addThread = function (fn) {
                threadArr.push(fn)
            }
            this.start = function () {     //启动  
                doingThread();   //执行doingThread函数
            }
            var doingThread = function () {
                if (threadArr.length > 0) {
                    if (nowTime < maxTime) {
                        let now = new Date().getTime(); //时间戳 毫秒级
                        var method = threadArr[0]; //获得第一个函数;
                        method(); //执行这个函数
                        threadArr.splice(0, 1); //数组函数  删除第一个元素
                        let nowNew = (new Date().getTime() - now);   //获得运行时间
                        nowTime += nowNew; //累加运行时间
                        doingThread();
                    } else {//每执行完线程后睡1ms
                        nowTime = 0;
                        setTimeout(doingThread, 1);
                    }
                } else {//先睡着等待线程队列
                    setTimeout(doingThread, 100);  //在指定的毫秒数后调用函数; 类似于递归函数,循环执行doingThread函数
                }
            }
        }
        var fn = function (num) {
            console.log(num)
        }
        var thread = new thread();
        thread.start();
        for (let i = 0; i < 2; i++) {
            thread.addThread(function () { fn(i);sleep(3000); }); //传递匿名函数到数组
        }

        function sleep(delay) { //延时函数
            var start = (new Date()).getTime();
            while ((new Date()).getTime() - start < delay) {
                continue;
            }
        }
    </script>

 https://www.cnblogs.com/charleswone/p/10228699.html

https://segmentfault.com/a/1190000008723632

 

setTimeout("test()","2000");  //2000毫秒后执行test()函数,只执行一次。
setInterval("test()","2000"); //每隔2000毫秒执行一次test()函数,执行无数次。
var interval = window.setInterval("test()","2000");
window.clearInterval(interval);     //停止执行setInterval循环。

当我们想让test()函数每隔2000毫秒执行一次,执行10000毫秒后停止执行时,可以用两者三者结合使用来实现。
var interval2 = window.setInterval("openit2()",2000);
setTimeout(function() {window.clearInterval(interval2);},10000);

带参方法执行延迟
setTimeout(function(){return executeQueryTask(data);},"10000");

https://www.cnblogs.com/tv151579/archive/2013/01/06/2848320.html

 

posted @ 2019-09-20 17:11  enych  阅读(361)  评论(0编辑  收藏  举报