一种js异步处理方式

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        (function () {
            let ctnTimer;
            function a() {
                let urls = [{
                    file: "a"
                }]
                setTimeout(()=>{
                    urls.forEach(item=>{
                        item.js = "1111";
                    })
                },500)
                b()
                function b() {
                    if (urls.some(function (url) {
                            return !url.js;
                        })) {
                        if (!ctnTimer) clearTimeout(ctnTimer);
                        ctnTimer = setTimeout(b, 500);
                        return;
                    } else {
                        console.log("1111111");
                        c();
                    }
                }
            }
            function c(){
                console.log("32222222222")
            }

            a();
        })()
    </script>
</body>

</html>

 

posted @ 2019-04-22 08:42  枫叶布  阅读(214)  评论(0编辑  收藏  举报