最新消息:

重写js原生方法

前端开发 luther 724浏览 0评论
新版本的浏览器setTimeout最低延时4ms,setInterval最低延时10ms;
function1();
setTimeyout(function2,100);//function2要等待function1();和function3();执行完才会执行。
function3();
<!DOCTYPE html>
<html lang="en">
<head>
<title>overwrite native function</title>
<metacharset="utf-8"/>
<metacontent="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"name="viewport">
</head>
<body style="padding: 0px; margin: 0px; background-color: rgb(243, 141, 16)">
<divid="cfather"></div>
</body>
<script>
   window.timeoutId = [];
        window.intervalId = [];
        var mySetTimeout = window.setTimeout;
        window.setTimeout = function (back, delay,p1,p2,p3,p4,p5) {
            let functionStr = back.toString();
            functionStr = functionStr.replace(new RegExp(" ", 'g'), "");
            //functionStr = functionStr.substring(0, 50);
            let tid = mySetTimeout(back, delay,p1,p2,p3,p4,p5)
            window.timeoutId.push({
                id:tid,
                "function": functionStr,
                "t": 0
            });
            return tid;
        };
        var mySetInterval = window.setInterval;
        window.setInterval = function (back, delay,p1,p2,p3,p4,p5) {
            let functionStr = back.toString();
            functionStr = functionStr.replace(new RegExp(" ", 'g'), "");
            functionStr = functionStr.substring(0, 50);
            let tid = mySetInterval(back, delay,p1,p2,p3,p4,p5);
            window.intervalId.push({
                id: tid,
                "function": functionStr,
                "t": 1
            });
            return tid;
        };
        var myClearInterval = clearInterval;
        var myClearTimeout = clearTimeout;
        window.clearInterval = function (tid) {
            if (tid==null || tid==undefined) return;
            console.log("clearInterval", tid);
            myClearInterval(tid);
            let intervalId_ = window.intervalId;
            for (let i = 0; i < intervalId_.length; i++) {
                if (window.intervalId[i].id == tid) {
                    window.intervalId.pop(i);
                    console.log("-------remove intervalId_ tid ----",tid);
                    break;
                }
            }
        };
        window.clearTimeout = function (tid) {
            if (tid==null || tid==undefined) return;
            console.log("clearTimeout", tid);
            myClearTimeout(tid);
            let timeoutid_ = window.timeoutId;
            for (let i = 0; i < timeoutid_.length; i++) {
                if (window.timeoutId[i].id == tid) {
                    console.log("-------remove timeoutid_ tid ----",tid);
                    window.timeoutId.pop(i);
                    break;
                }
            }
        };
        setInterval(function () {
            console.log("8888888888888888888888888888 timeoutlen:", window.timeoutId);
        }, 1000)
        setInterval(function () {
            console.log("9999999999999999999999999999 intervallen:", window.intervalId.length);
        }, 1000)
</script>
</html>

转载请注明:应用享评 » 重写js原生方法

您必须 登录 才能发表评论!