You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
979 B

2 years ago
  1. var i = 0;
  2. function mainFunc() {
  3. i++;
  4. //把i发送到浏览器的js引擎线程里
  5. postMessage(i);
  6. }
  7. var id = setInterval(mainFunc, 1);
  8. //<input type="text" name="ipt" id="ipt" value="" />
  9. //<button id="start">start</button>
  10. //<button id="stop">stop</button>
  11. //<button id="ale">alert</button>
  12. var ipt = document.getElementById("ipt");
  13. var stop = document.getElementById("stop");
  14. var start = document.getElementById("start");
  15. var ale = document.getElementById("ale");
  16. var worker = new Worker("/Scripts/TestWorker.js");
  17. worker.onmessage = function () {
  18. ipt.value = event.data;
  19. };
  20. stop.addEventListener("click", function () {
  21. //用于关闭worker线程
  22. worker.terminate();
  23. });
  24. start.addEventListener("click", function () {
  25. //开起worker线程
  26. worker = new Worker("/Scripts/TestWorker.js");
  27. worker.onmessage = function () {
  28. ipt.value = event.data;
  29. };
  30. });
  31. ale.addEventListener("click", function () {
  32. alert("i'm a dialog");
  33. });