Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

RADIO SHACK LEVEL II BASIC READY >_


dovenet / Synchronet Javascript / setInterval() and setTimeout()

SubjectAuthor
* setInterval() and setTimeout()Nightfox
+* setInterval() and setTimeout()Digital Man
|`- setInterval() and setTimeout()Nightfox
`* setInterval() and setTimeout()echicken
 `* setInterval() and setTimeout()Nightfox
  `* setInterval() and setTimeout()echicken
   +- setInterval() and setTimeout()Nightfox
   `- setInterval() and setTimeout()Nightfox

1
setInterval() and setTimeout()

<62158237.3556.dove_sync_js@digitaldistortionbbs.com>

  copy mid

https://www.rocksolidbbs.com/dovenet/article-flat.php?id=716&group=DOVE-Net.Synchronet_Javascript#716

  copy link   Newsgroups: DOVE-Net.Synchronet_Javascript
From: nightfox@VERT/DIGDIST (Nightfox)
To: Digital Man
Subject: setInterval() and setTimeout()
Message-ID: <62158237.3556.dove_sync_js@digitaldistortionbbs.com>
Date: Tue, 22 Feb 2022 09:39:19 -0800
X-Comment-To: Digital Man
Path: rocksolidbbs.com!not-for-mail
Organization: Digital Distortion
Newsgroups: DOVE-Net.Synchronet_Javascript
X-FTN-PID: Synchronet 3.19c-Win32 master/70898b5bb Feb 2 2022 MSC 1928
X-FTN-CHRS: CP437 2
WhenImported: 20220222165056-0800 41e0
WhenExported: 20220222165752-0800 41e0
ExportedFrom: VERT sync-js 3625
WhenImported: 20220222163919-0800 41e0
WhenExported: 20220222165051-0800 41e0
ExportedFrom: DIGDIST dove_sync_js 3556
Content-Type: text/plain; charset=IBM437
Content-Transfer-Encoding: 8bit
 by: Nightfox - Tue, 22 Feb 2022 17:39 UTC

Hi DM,

A couple of JS functions I noticed were added in Synchronet 3.19 are setTimeout() and setInterval(). If I understand it correctly, it sounds like these functions are meant to call a callback function after a specified period of time (with setInterval() doing so repeatedly)?

I'm currently running a Synchronet build from February 2, 2022. I tried making a simple test script to use those functions and see what happens, but it seems they aren't calling the callback functions I'm giving them. I'm not sure if I'm using them correctly:

var intervalID = js.setInterval(function() {
console.print("Interval function!\r\n");
}, 1000);
var timeoutID = js.setTimeout(function() {
console.print("Timeout function!\r\n");
}, 1000);
for (var i = 0; i < 10; ++i)
{ console.print("Loop!\r\n");
mswait(1000);
} js.clearInterval(intervalID);
js.clearTimeout(timeoutID);
console.print("Waiting....\r\n");
mswait(3000);
console.pause();

With that code, I see the "Loop!" output, but I don't see the output from the callbacks for setInterval() or setTimeout().

Also, the jsobjs.html page says setInterval() returns an object, but it actually seems to return a number (I outputted the type for the object I got back and it said it's a number).

Nightfox

---
■ Synchronet ■ Digital Distortion: digitaldistortionbbs.com

setInterval() and setTimeout()

<62159A4C.3626.sync-js@vert.synchro.net>

  copy mid

https://www.rocksolidbbs.com/dovenet/article-flat.php?id=717&group=DOVE-Net.Synchronet_Javascript#717

  copy link   Newsgroups: DOVE-Net.Synchronet_Javascript
From: digital.man@VERT (Digital Man)
To: Nightfox
Subject: setInterval() and setTimeout()
Message-ID: <62159A4C.3626.sync-js@vert.synchro.net>
Date: Tue, 22 Feb 2022 11:22:04 -0800
X-Comment-To: Nightfox
Path: rocksolidbbs.com!not-for-mail
Organization: Vertrauen
Newsgroups: DOVE-Net.Synchronet_Javascript
In-Reply-To: <62158237.3556.dove_sync_js@digitaldistortionbbs.com>
References: <62158237.3556.dove_sync_js@digitaldistortionbbs.com>
X-FTN-PID: Synchronet 3.19c-Win32 master/9d359582c Feb 22 2022 MSC 1929
X-FTN-CHRS: CP437 2
WhenImported: 20220222182204-0800 41e0
WhenExported: 20220223045754-0800 41e0
ExportedFrom: VERT sync-js 3626
Content-Type: text/plain; charset=IBM437
Content-Transfer-Encoding: 8bit
 by: Digital Man - Tue, 22 Feb 2022 19:22 UTC

Re: setInterval() and setTimeout()
By: Nightfox to Digital Man on Tue Feb 22 2022 04:39 pm

> Hi DM,
>
> A couple of JS functions I noticed were added in Synchronet 3.19 are
> setTimeout() and setInterval(). If I understand it correctly, it sounds
> like these functions are meant to call a callback function after a specified
> period of time (with setInterval() doing so repeatedly)?
>
> I'm currently running a Synchronet build from February 2, 2022. I tried
> making a simple test script to use those functions and see what happens, but
> it seems they aren't calling the callback functions I'm giving them. I'm
> not sure if I'm using them correctly:
>
> var intervalID = js.setInterval(function() {
> console.print("Interval function!\r\n");
> }, 1000);
> var timeoutID = js.setTimeout(function() {
> console.print("Timeout function!\r\n");
> }, 1000);
> for (var i = 0; i < 10; ++i)
> {
> console.print("Loop!\r\n");
> mswait(1000);
> }
> js.clearInterval(intervalID);
> js.clearTimeout(timeoutID);
> console.print("Waiting....\r\n");
> mswait(3000);
> console.pause();
>
> With that code, I see the "Loop!" output, but I don't see the output from
> the callbacks for setInterval() or setTimeout().

Deuce added those methods, I think for the ircd.js? So you'll probably need to reach out to him for some assistance. I haven't tried using those methods myself yet.

> Also, the jsobjs.html page says setInterval() returns an object, but it
> actually seems to return a number (I outputted the type for the object I got
> back and it said it's a number).

Yup, that looks like a doc bug. Thanks for the report.
--
digital man (rob)

Breaking Bad quote #9:
"Cheesedick" - I know that one [word]. How about that? - Hank Schrader
Norco, CA WX: 44.7°F, 88.0% humidity, 1 mph ESE wind, 0.00 inches rain/24hrs
---
■ Synchronet ■ Vertrauen ■ Home of Synchronet ■ [vert/cvs/bbs].synchro.net

setInterval() and setTimeout()

<62166833.3558.dove_sync_js@digitaldistortionbbs.com>

  copy mid

https://www.rocksolidbbs.com/dovenet/article-flat.php?id=718&group=DOVE-Net.Synchronet_Javascript#718

  copy link   Newsgroups: DOVE-Net.Synchronet_Javascript
From: nightfox@VERT/DIGDIST (Nightfox)
To: Digital Man
Subject: setInterval() and setTimeout()
Message-ID: <62166833.3558.dove_sync_js@digitaldistortionbbs.com>
Date: Wed, 23 Feb 2022 02:00:35 -0800
X-Comment-To: Digital Man
Path: rocksolidbbs.com!not-for-mail
Organization: Digital Distortion
Newsgroups: DOVE-Net.Synchronet_Javascript
In-Reply-To: <62159A4C.3626.sync-js@vert.synchro.net>
References: <62159A4C.3626.sync-js@vert.synchro.net>
X-FTN-PID: Synchronet 3.19c-Win32 master/70898b5bb Feb 2 2022 MSC 1928
X-FTN-CHRS: CP437 2
WhenImported: 20220223092842-0800 41e0
WhenExported: 20220223105757-0800 41e0
ExportedFrom: VERT sync-js 3627
WhenImported: 20220223090035-0800 41e0
WhenExported: 20220223092836-0800 41e0
ExportedFrom: DIGDIST dove_sync_js 3558
Content-Type: text/plain; charset=IBM437
Content-Transfer-Encoding: 8bit
 by: Nightfox - Wed, 23 Feb 2022 10:00 UTC

Re: setInterval() and setTimeout()
By: Digital Man to Nightfox on Tue Feb 22 2022 06:22 pm

DM> Deuce added those methods, I think for the ircd.js? So you'll probably
DM> need to reach out to him for some assistance. I haven't tried using those
DM> methods myself yet.

Thanks. I'll have a look at ircd.js (or see what other .js scripts may be using those functions).

Nightfox

---
■ Synchronet ■ Digital Distortion: digitaldistortionbbs.com

setInterval() and setTimeout()

<6218568E.3597.sync_js@bbs.electronicchicken.com>

  copy mid

https://www.rocksolidbbs.com/dovenet/article-flat.php?id=719&group=DOVE-Net.Synchronet_Javascript#719

  copy link   Newsgroups: DOVE-Net.Synchronet_Javascript
From: echicken@VERT/ECBBS (echicken)
To: Nightfox
Subject: setInterval() and setTimeout()
Message-ID: <6218568E.3597.sync_js@bbs.electronicchicken.com>
Date: Thu, 24 Feb 2022 21:09:50 -0500
X-Comment-To: Nightfox
Path: rocksolidbbs.com!not-for-mail
Organization: electronic chicken bbs
Newsgroups: DOVE-Net.Synchronet_Javascript
In-Reply-To: <62158237.3556.dove_sync_js@digitaldistortionbbs.com>
References: <62158237.3556.dove_sync_js@digitaldistortionbbs.com>
X-FTN-PID: Synchronet 3.19c-Linux master/9aaaef9ab Feb 22 2022 GCC 9.3.0
X-FTN-CHRS: CP437 2
WhenImported: 20220224201149-0800 41e0
WhenExported: 20220224225750-0800 41e0
ExportedFrom: VERT sync-js 3629
WhenImported: 20220225040950-0500 412c
WhenExported: 20220225041151Z 412c
ExportedFrom: ECBBS sync_js 3597
Content-Type: text/plain; charset=IBM437
Content-Transfer-Encoding: 8bit
 by: echicken - Fri, 25 Feb 2022 02:09 UTC

Re: setInterval() and setTimeout()
By: Nightfox to Digital Man on Tue Feb 22 2022 16:39:19

Ni> A couple of JS functions I noticed were added in Synchronet 3.19 are
Ni> setTimeout() and setInterval(). If I understand it correctly, it sounds

Ni> With that code, I see the "Loop!" output, but I don't see the output from
Ni> the callbacks for setInterval() or setTimeout().

If you add this:

js.do_callbacks = true;

your script won't terminate after it finishes its initial run. It enters a state analagous to the node.js event loop. Timeouts, intervals, and other callbacks will be fired as specified.

(You could probably put that line anywhere, but I put it at the bottom just as a matter of form.)

The "initial run" thing is important. None of your callbacks will be fired until the script has finished. This means that you should drop the 'for' loop and everything after it from your example script. They only serve to delay the script from finishing its run (and worse, clear the interval and timeout before they have a chance to be fired).

As you can probably imagine, you need to plan ahead and write your script according to a certain pattern if you plan to use this feature. At the moment, it's best used under jsexec or the services thread; I don't think there's adequate support for it in the terminal server context.

Ni> Also, the jsobjs.html page says setInterval() returns an object, but it
Ni> actually seems to return a number (I outputted the type for the object I
Ni> got back and it said it's a number).

It should probably say 'number', but bear in mind that this isn't a value that your script needs to inspect in any way. Its sole purpose is to identify a timer & callback according to whatever logic clearTimeout and clearInterval are following internally.

---
echicken
electronic chicken bbs - bbs.electronicchicken.com
---
■ Synchronet ■ electronic chicken bbs - bbs.electronicchicken.com

setInterval() and setTimeout()

<621861F8.3561.dove_sync_js@digitaldistortionbbs.com>

  copy mid

https://www.rocksolidbbs.com/dovenet/article-flat.php?id=720&group=DOVE-Net.Synchronet_Javascript#720

  copy link   Newsgroups: DOVE-Net.Synchronet_Javascript
From: nightfox@VERT/DIGDIST (Nightfox)
To: echicken
Subject: setInterval() and setTimeout()
Message-ID: <621861F8.3561.dove_sync_js@digitaldistortionbbs.com>
Date: Thu, 24 Feb 2022 13:58:32 -0800
X-Comment-To: echicken
Path: rocksolidbbs.com!not-for-mail
Organization: Digital Distortion
Newsgroups: DOVE-Net.Synchronet_Javascript
In-Reply-To: <6218568E.3597.sync_js@bbs.electronicchicken.com>
References: <6218568E.3597.sync_js@bbs.electronicchicken.com>
X-FTN-PID: Synchronet 3.19c-Win32 master/70898b5bb Feb 2 2022 MSC 1928
X-FTN-CHRS: CP437 2
WhenImported: 20220224210611-0800 41e0
WhenExported: 20220224225750-0800 41e0
ExportedFrom: VERT sync-js 3630
WhenImported: 20220224205832-0800 41e0
WhenExported: 20220224210608-0800 41e0
ExportedFrom: DIGDIST dove_sync_js 3561
Content-Type: text/plain; charset=IBM437
Content-Transfer-Encoding: 8bit
 by: Nightfox - Thu, 24 Feb 2022 21:58 UTC

Re: setInterval() and setTimeout()
By: echicken to Nightfox on Fri Feb 25 2022 04:09 am

Ni>> With that code, I see the "Loop!" output, but I don't see the output
Ni>> from
Ni>> the callbacks for setInterval() or setTimeout().

ec> If you add this:

ec> js.do_callbacks = true;

ec> your script won't terminate after it finishes its initial run. It enters a
ec> state analagous to the node.js event loop. Timeouts, intervals, and other
ec> callbacks will be fired as specified.

ec> (You could probably put that line anywhere, but I put it at the bottom
ec> just as a matter of form.)

ec> The "initial run" thing is important. None of your callbacks will be fired
ec> until the script has finished. This means that you should drop the 'for'
ec> loop and everything after it from your example script. They only serve to
ec> delay the script from finishing its run (and worse, clear the interval and
ec> timeout before they have a chance to be fired).

ec> As you can probably imagine, you need to plan ahead and write your script
ec> according to a certain pattern if you plan to use this feature. At the
ec> moment, it's best used under jsexec or the services thread; I don't think
ec> there's adequate support for it in the terminal server context.

Hmm.. I'm wondering if setInterval() and setTimeout() can be used as I expected. I was thinking I could use those to have a function be called while my script is doing other things. Say, for example, in SlyEdit, if I wanted it to udpate the time in the corner of the screen at a regular interval, I was thinking I could use setInterval() to have it run a function to do that at a regular interval (while the user is still editing a message). But with the behavior you describe, I'm not sure if that would be possible?

Nightfox

---
■ Synchronet ■ Digital Distortion: digitaldistortionbbs.com

setInterval() and setTimeout()

<6218729E.3599.sync_js@bbs.electronicchicken.com>

  copy mid

https://www.rocksolidbbs.com/dovenet/article-flat.php?id=721&group=DOVE-Net.Synchronet_Javascript#721

  copy link   Newsgroups: DOVE-Net.Synchronet_Javascript
From: echicken@VERT/ECBBS (echicken)
To: Nightfox
Subject: setInterval() and setTimeout()
Message-ID: <6218729E.3599.sync_js@bbs.electronicchicken.com>
Date: Thu, 24 Feb 2022 23:09:34 -0500
X-Comment-To: Nightfox
Path: rocksolidbbs.com!not-for-mail
Organization: electronic chicken bbs
Newsgroups: DOVE-Net.Synchronet_Javascript
In-Reply-To: <621861F8.3561.dove_sync_js@digitaldistortionbbs.com>
References: <621861F8.3561.dove_sync_js@digitaldistortionbbs.com>
X-FTN-PID: Synchronet 3.19c-Linux master/9aaaef9ab Feb 22 2022 GCC 9.3.0
X-FTN-CHRS: CP437 2
WhenImported: 20220224221144-0800 41e0
WhenExported: 20220224225750-0800 41e0
ExportedFrom: VERT sync-js 3631
WhenImported: 20220225060934-0500 412c
WhenExported: 20220225061150Z 412c
ExportedFrom: ECBBS sync_js 3599
Content-Type: text/plain; charset=IBM437
Content-Transfer-Encoding: 8bit
 by: echicken - Fri, 25 Feb 2022 04:09 UTC

Re: setInterval() and setTimeout()
By: Nightfox to echicken on Thu Feb 24 2022 20:58:32

Ni> Hmm.. I'm wondering if setInterval() and setTimeout() can be used as I
Ni> expected. I was thinking I could use those to have a function be called
Ni> while my script is doing other things. Say, for example, in SlyEdit, if I
Ni> wanted it to udpate the time in the corner of the screen at a regular
Ni> interval, I was thinking I could use setInterval() to have it run a
Ni> function to do that at a regular interval (while the user is still editing
Ni> a message). But with the behavior you describe, I'm not sure if that would
Ni> be possible?

I don't think it's possible yet.

You're probably taking input from the user via console.getkey inside of a loop. You may be doing this in a single main loop, or in several places. Any such loop will block the script and prevent timers from running.

The solution would be to have your script define a bunch of functions, etc., register a callback that gets fired when the user hits a key, and then simply exit without blocking / looping. At this point everything flows from your timers and input handler. Now your timers are free to run, and won't be blocked except maybe briefly by your input callback. Unless SlyEdit was designed in a very modular way, this probably means a huge refactoring.

Even if you had the appetite to do this work, there's a catch: there is no "user input" event, and no way to register that crucial callback. You could probably fake this by using console.inkey in a setInterval, but that's getting pretty nasty.

I'm using SlyEdit right now, and I noticed that if I pause for a minute, the clock doesn't update until I hit a key. So I imagine you're using console.getkey. Here's an easier solution to updating the clock on a schedule:

load('event-timer.js');

const t = new Timer();

function updateClock() {
// do stuff here
}

function getKey() {
var k;
while (!k && !js.terminated) {
t.cycle();
k = console.inkey(K_NONE, 25);
}
return k;
}

t.addEvent(1000, true, updateClock);

Obviously simplistic, but the idea would be to use the custom getKey function as a drop-in replacement for console.getkey. Now you've got a timer that will be allowed to run while you wait for user input.

---
echicken
electronic chicken bbs - bbs.electronicchicken.com
---
■ Synchronet ■ electronic chicken bbs - bbs.electronicchicken.com

setInterval() and setTimeout()

<62190C6A.3563.dove_sync_js@digitaldistortionbbs.com>

  copy mid

https://www.rocksolidbbs.com/dovenet/article-flat.php?id=722&group=DOVE-Net.Synchronet_Javascript#722

  copy link   Newsgroups: DOVE-Net.Synchronet_Javascript
From: nightfox@VERT/DIGDIST (Nightfox)
To: echicken
Subject: setInterval() and setTimeout()
Message-ID: <62190C6A.3563.dove_sync_js@digitaldistortionbbs.com>
Date: Fri, 25 Feb 2022 02:05:46 -0800
X-Comment-To: echicken
Path: rocksolidbbs.com!not-for-mail
Organization: Digital Distortion
Newsgroups: DOVE-Net.Synchronet_Javascript
In-Reply-To: <6218729E.3599.sync_js@bbs.electronicchicken.com>
References: <6218729E.3599.sync_js@bbs.electronicchicken.com>
X-FTN-PID: Synchronet 3.19c-Win32 master/70898b5bb Feb 2 2022 MSC 1928
X-FTN-CHRS: CP437 2
WhenImported: 20220225090826-0800 41e0
WhenExported: 20220225105751-0800 41e0
ExportedFrom: VERT sync-js 3632
WhenImported: 20220225090546-0800 41e0
WhenExported: 20220225090822-0800 41e0
ExportedFrom: DIGDIST dove_sync_js 3563
Content-Type: text/plain; charset=IBM437
Content-Transfer-Encoding: 8bit
 by: Nightfox - Fri, 25 Feb 2022 10:05 UTC

Re: setInterval() and setTimeout()
By: echicken to Nightfox on Fri Feb 25 2022 06:09 am

Ni>> setInterval() to have it run a function to do that at a regular
Ni>> interval (while the user is still editing a message). But with the
Ni>> behavior you describe, I'm not sure if that would be possible?

ec> I don't think it's possible yet.

ec> You're probably taking input from the user via console.getkey inside of a
ec> loop. You may be doing this in a single main loop, or in several places.
ec> Any such loop will block the script and prevent timers from running.

ec> The solution would be to have your script define a bunch of functions,
ec> etc., register a callback that gets fired when the user hits a key, and
ec> then simply exit without blocking / looping. At this point everything
ec> flows from your timers and input handler. Now your timers are free to run,
ec> and won't be blocked except maybe briefly by your input callback. Unless
ec> SlyEdit was designed in a very modular way, this probably means a huge
ec> refactoring.

Yeah, that would probably be a bit too much of a refactoring right now. I didn't know how Synchronet implements setInterval() or setTimeout() but I wondered if Synchronet may be starting a separate thread to wait and call your function, but it sounds like it's not implemented that way.

ec> I'm using SlyEdit right now, and I noticed that if I pause for a minute,
ec> the clock doesn't update until I hit a key. So I imagine you're using
ec> console.getkey.

Yeah, for each keypress, SlyEdit just checks the current time and if it's different from the displayed time, it will update the time on the screen.

ec> Here's an easier solution to updating the clock on a
ec> schedule:

ec> load('event-timer.js');

ec> const t = new Timer();

ec> function updateClock() {
ec> // do stuff here
ec> }

ec> function getKey() {
ec> var k;
ec> while (!k && !js.terminated) {
ec> t.cycle();
ec> k = console.inkey(K_NONE, 25);
ec> }
ec> return k;
ec> }

ec> t.addEvent(1000, true, updateClock);

ec> Obviously simplistic, but the idea would be to use the custom getKey
ec> function as a drop-in replacement for console.getkey. Now you've got a
ec> timer that will be allowed to run while you wait for user input.

It looks like that would be a good solution.

Nightfox

---
■ Synchronet ■ Digital Distortion: digitaldistortionbbs.com

setInterval() and setTimeout()

<62190CE2.3564.dove_sync_js@digitaldistortionbbs.com>

  copy mid

https://www.rocksolidbbs.com/dovenet/article-flat.php?id=723&group=DOVE-Net.Synchronet_Javascript#723

  copy link   Newsgroups: DOVE-Net.Synchronet_Javascript
From: nightfox@VERT/DIGDIST (Nightfox)
To: echicken
Subject: setInterval() and setTimeout()
Message-ID: <62190CE2.3564.dove_sync_js@digitaldistortionbbs.com>
Date: Fri, 25 Feb 2022 02:07:46 -0800
X-Comment-To: echicken
Path: rocksolidbbs.com!not-for-mail
Organization: Digital Distortion
Newsgroups: DOVE-Net.Synchronet_Javascript
In-Reply-To: <6218729E.3599.sync_js@bbs.electronicchicken.com>
References: <6218729E.3599.sync_js@bbs.electronicchicken.com>
X-FTN-PID: Synchronet 3.19c-Win32 master/70898b5bb Feb 2 2022 MSC 1928
X-FTN-CHRS: CP437 2
WhenImported: 20220225090826-0800 41e0
WhenExported: 20220225105751-0800 41e0
ExportedFrom: VERT sync-js 3633
WhenImported: 20220225090746-0800 41e0
WhenExported: 20220225090822-0800 41e0
ExportedFrom: DIGDIST dove_sync_js 3564
Content-Type: text/plain; charset=IBM437
Content-Transfer-Encoding: 8bit
 by: Nightfox - Fri, 25 Feb 2022 10:07 UTC

Re: setInterval() and setTimeout()
By: echicken to Nightfox on Fri Feb 25 2022 06:09 am

ec> const t = new Timer();

Also I wasn't aware of this Timer. I see it's defined in load/event-timer.js. Thanks for pointing me to that.

Nightfox

---
■ Synchronet ■ Digital Distortion: digitaldistortionbbs.com

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor