“Pekerja Web di JavaScript” Kode Jawaban

Apa itu WebWorker

web worker
Web Workers are a mechanism to create a separate thread 
in parallel to your main JS program thread. 
can only communicate with each other through normal async events 
always abide by the event-loop one-at-a-time behavior 
required by run-to-completion.
Pink Person

Pekerja Web di JavaScript



let start = document.querySelector('#start') //it is a button
let stop = document.querySelector('#stop') //it is a button
let show = document.querySelector('#show')



let output
start.addEventListener('click', () => {

    //Before creating a web worker, check whether the user's browser supports it:
    if (typeof Worker !== 'undefined') {

        //check web worker whether undefined or not
        if (typeof output === 'undefined') {
            output = new Worker('looping.js') //(looping.js) is another file that you can processing anything
        }

        output.onmessage = (event) => {
            show.innerHTML = event.data
        }
    } else {
        alert('Sorry web worker dose not support')
    }
})


stop.addEventListener('click', () => {
    if (typeof output !== 'undefined') {
        output.terminate();
        output = undefined
    } else {
        lert('Sorry web worker dose not support')
    }
})
Mohammad Nazim

Jawaban yang mirip dengan “Pekerja Web di JavaScript”

Pertanyaan yang mirip dengan “Pekerja Web di JavaScript”

Lebih banyak jawaban terkait untuk “Pekerja Web di JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya