Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: Serialise setInterval Date: Thu, 20 May 2021 03:00:03 +0100 Organization: A noiseless patient Spider Lines: 28 Message-ID: <87bl96w4f0.fsf@bsb.me.uk> References: <6b1b8917-b6f8-42bd-bf1a-ac18d7c70624n@googlegroups.com> <80fe2283-d42a-4205-bd90-5189e1ed5447n@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Injection-Info: reader02.eternal-september.org; posting-host="40361b37221cb96cb28693c773a02343"; logging-data="27806"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18+1Xv+1TDnsiYbHdq7V/gtFMMh1k9Jyuc=" Cancel-Lock: sha1:STtMN8rpxP382NHTyjCLTz+sKeE= sha1:jBPa1n0AGoKzINuqlk0JGBetCkk= X-BSB-Auth: 1.2a9df388cd39680c5995.20210520030003BST.87bl96w4f0.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:40059 Jonas Thörnvall writes: > onsdag 19 maj 2021 kl. 22:05:19 UTC+2 skrev Jonas Thörnvall: >> Can someone give a minimalistic example on howto serialise two setIntervals? >> "To some sort of accuracy" > Can someone tell why this approach would not work? > function countIn(){ > REALTIME=(performance.now()-browserLoadTIME)-2400; > if (REALTIME<0){ > stopCount=setInterval(countIn,600); > } else { > clearInterval(stopCount); > stopRec=setInterval(recSomeNotes,5); > } > } This looks very odd. It's possible that you create multiple interval timers (before you find that REALTIME >= 0), but you only store the ID of the last one, so all but that one can't be cancelled. It appears that you want to keep checking some time to take action later in the else clause. Can't you calculate when REALTIME will become >= 0? Then you could use setTimeout. Please learn to use local variables. -- Ben.