• setInterval() and setTimeout()

    From echicken@VERT/ECBBS to Nightfox on Friday, February 25, 2022 04:09:50
    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