Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #40074

Re: Serialise setInterval

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.javascript
Subject Re: Serialise setInterval
Date 2021-05-20 16:52 +0100
Organization A noiseless patient Spider
Message-ID <87zgwps8pw.fsf@bsb.me.uk> (permalink)
References (4 earlier) <53eab967-363f-494a-a9d2-cc08620b670en@googlegroups.com> <720fbe37-8667-47e7-b73f-adc20f555836n@googlegroups.com> <a4546b64-623c-45a6-8855-d62083e94e7an@googlegroups.com> <87a6optqde.fsf@bsb.me.uk> <89332481-108f-48d2-8487-fc059757b6een@googlegroups.com>

Show all headers | View raw


Jonas Thörnvall <jonas.thornvall@gmail.com> 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.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Serialise setInterval Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-05-19 13:05 -0700
  Re: Serialise setInterval Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-05-19 13:42 -0700
    Re: Serialise setInterval Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-05-20 03:00 +0100
      Re: Serialise setInterval Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-05-20 05:20 -0700
      Re: Serialise setInterval Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-05-20 05:22 -0700
      Re: Serialise setInterval Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-05-20 05:25 -0700
        Re: Serialise setInterval Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-05-20 05:28 -0700
          Re: Serialise setInterval Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-05-20 05:36 -0700
            Re: Serialise setInterval Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-05-20 05:43 -0700
              Re: Serialise setInterval Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-05-20 15:46 +0100
                Re: Serialise setInterval Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-05-20 08:13 -0700
                Re: Serialise setInterval Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-05-20 16:52 +0100
                Re: Serialise setInterval Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-05-20 09:11 -0700
                Re: Serialise setInterval Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-05-20 09:17 -0700
                Re: Serialise setInterval John G Harris <niam@jghnorth.org.uk.invalid> - 2021-05-20 18:40 +0100
                Re: Serialise setInterval Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-05-20 15:31 -0700
                Re: Serialise setInterval Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-05-21 15:12 -0700
                Re: Serialise setInterval Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-05-21 15:14 -0700
            Re: Serialise setInterval Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-05-20 05:55 -0700

csiph-web