Subj : setInterval() and setTimeout()
To   : echicken
From : Nightfox
Date : Fri Feb 25 2022 09:05 am

 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