threadTimeouts_worker.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/
  4. */
  5. var gTimeoutId;
  6. var gTimeoutCount = 0;
  7. var gIntervalCount = 0;
  8. function timeoutFunc() {
  9. if (++gTimeoutCount > 1) {
  10. throw new Error("Timeout called more than once!");
  11. }
  12. postMessage("timeoutFinished");
  13. }
  14. function intervalFunc() {
  15. if (++gIntervalCount == 2) {
  16. postMessage("intervalFinished");
  17. }
  18. }
  19. function messageListener(event) {
  20. switch (event.data) {
  21. case "startTimeout":
  22. gTimeoutId = setTimeout(timeoutFunc, 2000);
  23. clearTimeout(gTimeoutId);
  24. gTimeoutId = setTimeout(timeoutFunc, 2000);
  25. break;
  26. case "startInterval":
  27. gTimeoutId = setInterval(intervalFunc, 2000);
  28. break;
  29. case "cancelInterval":
  30. clearInterval(gTimeoutId);
  31. postMessage("intervalCanceled");
  32. break;
  33. case "startExpression":
  34. setTimeout("this.postMessage('expressionFinished');", 2000);
  35. break;
  36. default:
  37. throw "Bad message: " + event.data;
  38. }
  39. }
  40. addEventListener("message", messageListener, false);