• HTML5-WebWorker



    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>HTML5-WebWorker</title>
        </head>
        <body>
            
            web worker实时时间:<div id="workerTime"></div>
            <br />
            主线程获取当前时间:<div id="curTime"></div>
            <button onclick="mainthread()">主线程获取时间</button>
        
            <script type="text/javascript">
                /*
                 * Web worker的运行原理和两种线程的详细说明 : http://www.ibm.com/developerworks/cn/web/1112_sunch_webworker/
                 */
                var interval;
                if (typeof Worker != undefined) {
                    var worker = new Worker("js/webworker.js");
                    worker.onmessage = function (event)
                    {
                        document.getElementById("workerTime").textContent = event.data;
                    }
                    interval = setInterval('worker.postMessage("1000000")', 1000);
                }
        
                function mainthread() {
                    document.getElementById("curTime").textContent = new Date();
                }
                function stop() {
                    clearInterval(interval);
               worker.terminate();
                }
                setTimeout(stop, 60000);//60秒之后清理interval
            </script>
            
        </body>
    </html>

    webworker.js

    onmessage = function(event) {
        var msg = event.data;
        for (var i = 0; i < msg; i++) {
            if (!!console && i % 500 == 0) {
                console.info(i);
            }
        }
        var d = new Date();
        postMessage(d);
    }
  • 相关阅读:
    bzoj4028 [HEOI2015]公约数数列
    bzoj4766 文艺计算姬
    bzoj4241 历史研究
    bzoj3744 Gty的妹子序列
    bzoj4540 [Hnoi2016]序列
    uoj#228 基础数据结构练习题
    bzoj2467 [中山市选2010]生成树
    bzoj2125 最短路
    bzoj4800 [Ceoi2015]Ice Hockey World Championship
    bzoj2463 [中山市选2009]谁能赢呢?
  • 原文地址:https://www.cnblogs.com/blog-leo/p/4457707.html
Copyright © 2020-2023  润新知