Skip to content

Commit

Permalink
ajusta exemplos de worker
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmdantas committed Oct 17, 2015
1 parent 04fcb03 commit c50a628
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
15 changes: 9 additions & 6 deletions com_web_worker/app2.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
var wk = new Worker('web_worker.js');
var algumArray = [1, 2, 3];

wk.addEventListener('message', ({data}) => {
console.log(data); // [10, 20, 30]
});
document.getElementById('btn-processo').addEventListener('click', () => {
document.getElementById('resultado').innerText = 'carregando';

document.getElementById('').addEventListener('click', () => {
wk.postMessage(algumArray); // [1, 2, 3]
});
wk.addEventListener('message', (ev) => {
console.log(ev.data.array); // [10, 20, 30]
document.getElementById('resultado').innerText = ev.data.soma;
});

wk.postMessage(algumArray); // [1, 2, 3]
})
6 changes: 3 additions & 3 deletions com_web_worker/index2.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

<body>
<main>
<button type="button">processa algo lento</button>
<button type="button" id="btn-processo">processa algo lento</button>
<p>resultado: <span id="resultado"></span> </p>
</main>

<script src="app2.js"></script>
<script src="web_worker.js"></script>
<script src="./app2.js"></script>
<script src="./web_worker.js"></script>
</body>
</html>
13 changes: 9 additions & 4 deletions com_web_worker/web_worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
self.onmessage = ({data}) => {
var resultArray = data.map((i) => i * 10);

postMessage(resultArray); // posting [10, 20, 30]
self.onmessage = (ev) => {
var resultArray = ev.data.map((i) => i * 10);
var soma = 0;

resultArray.forEach((r) => {
soma += r;
});

postMessage({array: resultArray, soma: soma});
}
14 changes: 13 additions & 1 deletion sem_web_worker/app1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
var algumArray = [1, 2, 3];
var resultadoAlgumArray = algumArray.map((i) => i * 10);
var soma = 0;

console.log(resultadoAlgumArray); // [10, 20, 30]
document.getElementById('btn-processo').addEventListener('click', () => {
document.getElementById('resultado').innerText = 'carregando';

resultadoAlgumArray.forEach((r) => {
soma += r;
});

console.log(resultadoAlgumArray); // [10, 20, 30]
console.log(soma); // [10, 20, 30]

document.getElementById('resultado').innerText = soma;
})
4 changes: 2 additions & 2 deletions sem_web_worker/index1.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

<body>
<main>
<button type="button">processa algo lento</button>
<button type="button" id="btn-processo">processa algo lento</button>
<p>resultado: <span id="resultado"></span> </p>
</main>

<script src="app1.js"></script>
<script type="text/javascript" src="./app1.js"></script>
</body>
</html>

0 comments on commit c50a628

Please sign in to comment.