code: function WorkerFn(root) {
    var timers = {};
  
    root.onmessage = function (event) {
      switch (event.data.type) {
        case 'ping':
          root.postMessage({ type: 'pong' });
          break;
  
        case 'set_timeout':
          timers[event.data.id] = setTimeout(function () {
            root.postMessage({ type: 'tick', id: event.data.id });
          }, event.data.delay);
          break;
  
        case 'clear_timeout':
          clearTimeout(timers[event.data.id]);
          break;
  
        case 'set_interval':
          timers[event.data.id] = setInterval(function () {
            root.postMessage({ type: 'tick', id: event.data.id });
          }, event.data.interval);
          break;
  
        case 'clear_interval':
          clearInterval(timers[event.data.id]);
          break;
      }
    };
  }
docs: ///////////////////////////////////////////////////////////////////////////// Manage timers in web worker