• setInterval() and setTimeout()

    From Nightfox@VERT/DIGDIST to Digital Man on Tue Feb 22 16:39:19 2022
    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
  • From Digital Man@VERT to Nightfox on Tue Feb 22 18:22:04 2022
    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
  • From Nightfox@VERT/DIGDIST to Digital Man on Wed Feb 23 09:00:35 2022
    Re: setInterval() and setTimeout()
    By: Digital Man to Nightfox on Tue Feb 22 2022 06:22 pm

    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.

    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
  • From echicken@VERT/ECBBS to Nightfox on Fri Feb 25 04:09:50 2022
    Re: setInterval() and setTimeout()
    By: Nightfox to Digital Man on Tue Feb 22 2022 16:39:19

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

    With that code, I see the "Loop!" output, but I don't see the output from 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.

    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).

    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
  • From Nightfox@VERT/DIGDIST to echicken on Thu Feb 24 20:58:32 2022
    Re: setInterval() and setTimeout()
    By: echicken to Nightfox on Fri Feb 25 2022 04:09 am

    With that code, I see the "Loop!" output, but I don't see the output
    from
    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.

    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
  • From echicken@VERT/ECBBS to Nightfox on Fri Feb 25 06:09:34 2022
    Re: setInterval() and setTimeout()
    By: Nightfox to echicken on Thu Feb 24 2022 20:58:32

    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?

    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
  • From Nightfox@VERT/DIGDIST to echicken on Fri Feb 25 09:05:46 2022
    Re: setInterval() and setTimeout()
    By: echicken to Nightfox on Fri Feb 25 2022 06:09 am

    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?

    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.

    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.

    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.

    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.

    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.

    It looks like that would be a good solution.

    Nightfox

    ---
    þ Synchronet þ Digital Distortion: digitaldistortionbbs.com
  • From Nightfox@VERT/DIGDIST to echicken on Fri Feb 25 09:07:46 2022
    Re: setInterval() and setTimeout()
    By: echicken to Nightfox on Fri Feb 25 2022 06:09 am

    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