To create a multi-threaded web worker

// ------------------------------------------ main thread contents of the file:
btn.onclick=function(){
            var number=input.value;

            Main thread // 1. Start worker using the Worker
            var worker=new Worker('work.js');
            2. // send data to the number of threads points
            worker.postMessage(number);
            // 5. monitor division thread returns data
            worker.onmessage=function(event){
                var result=event.data;
            }
        }
// ---------------------------------------- sub-thread file work.js file contents :
function fn(n){
    return n<=2 ? 1:fn(n-1)+fn(n-2);
}
var onmessage=function(event){
    // 3 minutes thread gets sent over the main thread number
    var number=event.data;
    4. // calculated number, and sends the calculation result to the main thread
    were result = fn (number);
    postMessage(result);
}

Guess you like

Origin www.cnblogs.com/laidans/p/10993795.html