“веб -работник в JavaScript” Ответ

Что такое веб -работник

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

веб -работник в 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

Ответы похожие на “веб -работник в JavaScript”

Вопросы похожие на “веб -работник в JavaScript”

Больше похожих ответов на “веб -работник в JavaScript” по JavaScript

Смотреть популярные ответы по языку

Смотреть другие языки программирования