其他分享
首页 > 其他分享> > 前端-JS-worker-ie10及以上

前端-JS-worker-ie10及以上

作者:互联网

//主线程
var worker = new Worker("worker.js");
worker.onmessage = function(event){
    switch (event.data.type){
        case "debug":
            console.log(event.data.message);
            break;

        //other types of data
    }
};

worker.postMessage({
    type: "start",
    value: 12345
});

//web worker.js
self.onmessage = function(event){
    if (event.data.type == "start"){
        process(event.data.value);
    }
};

function process(number){

    self.postMessage({
        type: "debug",
        message: "Starting processing..."
    });

    //code

    self.postMessage({
        type: "debug",
        message: "Processing finished"
    });
}

标签:data,self,worker,JS,ie10,message,type,event
来源: https://www.cnblogs.com/xcc3306/p/13495109.html