-
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ãã 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ã
-
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ãã 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ã
-
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ãã 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ã
-
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ãã 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ã
-
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ãã 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ã
-
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ãã 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ã