var i=0; var update = null; function timedCount(){ onmessage = (e) => { if(e.data == "on"){ console.log("on"); update = setInterval("stationupdate()", 2000); } if(e.data == "off"){ clearInterval(update); } } } function stationupdate(){ postMessage("up"); } timedCount();