使用worker模拟售票系统

2016-12-21  本文已影响0人  挣脱吧小白

1、代码:

    <script type="text/javascript">
    //创建一个空数组存放worker

    var btn = document.getElementById("button");   
    btn.onclick = function(){
        //1.判断当前浏览器是否支持worker
        if(typeof(Worker) !== "undefined"){
            //2.因为每个窗口购票互不影响,所以创建5个worker
            for(var i=0;i<5;i++){
                var worker = new Worker("getTicket.js");                    
                worker.onmessage = getMessage;
                //将worker放到数组里面
                workers.push(worker);   
            }       
        }       
        function getMessage(event){
            //获取到事件目标对象
            var w = event.target;   
            var index = workers.indexOf(w); 
            var win = document.getElementById("win"+index); 
            if(event.data == 1){    
               win.className = "hasticket";
            }else{  
                win.className = "noTicket"; 
            }   
        }       
    }
</script>

2、实现效果图:

Paste_Image.png
上一篇下一篇

猜你喜欢

热点阅读