您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TimerWorker.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * SET_INTERVAL constant is used to set interval and it is set in
  3. * the id property of the request.data property. timeMs property must
  4. * also be set. request.data example:
  5. *
  6. * {
  7. * id: SET_INTERVAL,
  8. * timeMs: 33
  9. * }
  10. */
  11. export const SET_INTERVAL = 2;
  12. /**
  13. * CLEAR_INTERVAL constant is used to clear the interval and it is set in
  14. * the id property of the request.data property.
  15. *
  16. * {
  17. * id: CLEAR_INTERVAL
  18. * }
  19. */
  20. export const CLEAR_INTERVAL = 3;
  21. /**
  22. * INTERVAL_TIMEOUT constant is used as response and it is set in the id property.
  23. *
  24. * {
  25. * id: INTERVAL_TIMEOUT
  26. * }
  27. */
  28. export const INTERVAL_TIMEOUT = 22;
  29. /**
  30. * The following code is needed as string to create a URL from a Blob.
  31. * The URL is then passed to a WebWorker. Reason for this is to enable
  32. * use of setInterval that is not throttled when tab is inactive.
  33. */
  34. const code
  35. = ` let timer = null;
  36. onmessage = function(request) {
  37. switch (request.data.id) {
  38. case ${SET_INTERVAL}: {
  39. timer = setInterval(() => {
  40. postMessage({ id: ${INTERVAL_TIMEOUT} });
  41. }, request.data.timeMs);
  42. break;
  43. }
  44. case ${CLEAR_INTERVAL}: {
  45. clearInterval(timer);
  46. break;
  47. }
  48. }
  49. };
  50. `;
  51. const blob = new Blob([ code ], { type: 'application/javascript' });
  52. export const timerWorkerScript = URL.createObjectURL(blob);