Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. var first = document.querySelector('#number1');
  2. var second = document.querySelector('#number2');
  3. var result = document.querySelector('.result');
  4.  
  5. if (window.Worker) { // Check if Browser supports the Worker api.
  6. var myWorker = new Worker("worker.js");
  7.  
  8. // onkeyup could be used instead of onchange if you wanted to update the answer every time
  9. // an entered value is changed, and you don't want to have to unfocus the field to update its .value
  10.  
  11. first.onchange = function() {
  12. myWorker.postMessage([first.value,second.value]); // Sending message as an array to the worker
  13. console.log('Message posted to worker');
  14. };
  15.  
  16. second.onchange = function() {
  17. myWorker.postMessage([first.value,second.value]);
  18. console.log('Message posted to worker');
  19. };
  20.  
  21. myWorker.onmessage = function(e) {
  22. result.textContent = e.data;
  23. console.log('Message received from worker');
  24. };
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement