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 16:52:59 +0100 Organization: A noiseless patient Spider Lines: 90 Message-ID: <87zgwps8pw.fsf@bsb.me.uk> References: <6b1b8917-b6f8-42bd-bf1a-ac18d7c70624n@googlegroups.com> <80fe2283-d42a-4205-bd90-5189e1ed5447n@googlegroups.com> <87bl96w4f0.fsf@bsb.me.uk> <513d1eb1-651c-4624-aef7-8a2ca455ede3n@googlegroups.com> <53eab967-363f-494a-a9d2-cc08620b670en@googlegroups.com> <720fbe37-8667-47e7-b73f-adc20f555836n@googlegroups.com> <87a6optqde.fsf@bsb.me.uk> <89332481-108f-48d2-8487-fc059757b6een@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="25352"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Lkkq3Tr3u+ImbQPmFhFmbwu4BsXVzfyI=" Cancel-Lock: sha1:ZuR8lAq2q6slfc2gp0gkyX8QUQI= sha1:bylEOp2Fg8qoLuEPKj30Vg6NK1Q= X-BSB-Auth: 1.d45d4e0b8857f79c03bb.20210520165259BST.87zgwps8pw.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:40074 Jonas Thörnvall writes: > torsdag 20 maj 2021 kl. 16:46:29 UTC+2 skrev Ben Bacarisse: >> console.log("Wait for it..."); >> >> const longTimer = setInterval( >> () => { >> clearInterval(longTimer); >> console.log("go"); >> var nTicks = 0; >> const shortTimer = setInterval( >> () => { >> if (nTicks++ >= 5) >> clearInterval(shortTimer); >> else console.log("tick"); >> }, 500); >> }, 2000); My code was indented. Maybe Google broke that as well... > Yeah thank you Ben, but where did good ol meta code go. Why does > javascript code look so contrieved, two sequentail things nested in > eachother LoL, it would be hilarious if it was not deranged. It's not deranged. It's designed like that for very good reason, but I'm not going to get into that. > And things like const longTimer = setInterval( () => { how do one > even come up with such concepts and what is it supposed to mean? Which bit? I want a local variable for the returned timer ID. I'm never going to change it so I'll let everyone know that by declaring it const. The two things that look like this: () => { ... code ... } and simply anonymous functions. They are very similar to functions sometimes seen in older code created like this function () { ... code ... } They are often used for call-backs because you don't usually need to create a named function when it's going to used in just one place. > Is it setInterval with some sort of inlined function between clause? Err... maybe, but inlined doesn't mean what you think it means. > And again why is the two intervals calls nested in eachother, is it > basicly the same as i do splitting the second call thru the else but > you do the second thing inside the first I don't know what you mean. Here is the same program written with named functions. Note that I've still decided to limit the scope of the name of the shortCallback function so it can have exclusive access to the counter: console.log("Wait for it..."); const longTimer = setInterval(longCallback, 2000); function longCallback() { clearInterval(longTimer); console.log("go"); var nTicks = 0; const shortTimer = setInterval(shortCallback, 500); function shortCallback() { if (nTicks++ >= 5) clearInterval(shortTimer); else console.log("tick"); } } > Can someone honestly say that doing two consecutive processes in this > way is not contrieved? Yes, there are very good reasons for it. Unfortunately I'm not convinced that these setTimeout calls are the right way to do whatever it is you are trying to do. > And thanks again Ben, i think i understand the code does not really mean i do. > Do you think we can get a switch on setInterval and setTimeout to run > them single threaded...... They already run single-threaded. -- Ben.