Welcome to the Achaea Forums! Please be sure to read the Forum Rules.

[Nexus] Temp Timer Help

AquilAquil Member Posts: 269 ✭✭✭✭ - Eminent
edited March 2016 in Client Help
Hi! I'm a total noob to Nexus as I'm a mudlet user but trying to help someone who uses Nexus. Any way to make an echo or something appear every minute or so through Nexus like you can in Mudlet?

 tempTimer(math.random(30,120) [[send("Wild Pikachu Appears")]])

 Any help is appreciated!

Comments

  • AntoniusAntonius Member Posts: 5,040 @@ - Legendary Achaean

    JavaScript has the setTimeout function that can be used to delay arbitrary code by a specified amount of time. You'd use it like this:

    
    var myTimeout = setTimeout(X, function() {
        // your code here, I don't know what the Nexus equivalent of send is
    });
    

    There's also setInterval which works in much the same way, but will repeat the passed function repeatedly on the interval specified, rather than a single time like setTimeout.

    Note: You can pass a string of code to setTimeout, but you really, really shouldn't.

    Aquil
  • AquilAquil Member Posts: 269 ✭✭✭✭ - Eminent
    Antonius said:

    JavaScript has the setTimeout function that can be used to delay arbitrary code by a specified amount of time. You'd use it like this:

    var myTimeout = setTimeout(X, function() {
        // your code here, I don't know what the Nexus equivalent of send is
    });
    

    There's also setInterval which works in much the same way, but will repeat the passed function repeatedly on the interval specified, rather than a single time like setTimeout.

    Note: You can pass a string of code to setTimeout, but you really, really shouldn't.

    So this works for the new Nexus client? Like they could add in an emote to go off every 5 minutes for like.. Ambience or something? Kinda like what Morthif does with Orthold (spelling?)
  • TaelTael Member Posts: 1,197 @ - Epic Achaean
    edited March 2016
    The Nexus client is just JavaScript and setTimeout is a normal JavaScript function.

    You pass it a time (in milliseconds) and a function to run at the end of that time.

    So you can either just define an anonymous function inside it like Antonius has or even pass it a function defined elsewhere.

    So I might have a function called say_hello somewhere that looks like this:
    function say_hello() {
        send_command("say Hello!", true);
    }
    Note the true there - when you use send_command in Nexus, you pretty much always want to use the true there so it just sends the command instead of checking to see if it matches an aliases first.

    And then if I wanted to automatically do that two minutes after some alias or something, I would do:
    setTimeout(120000, say_hello);
    If you pass more things to setTimeout, it passes them as an argument to the function, so you could alternatively do:
    setTimeout(12000, send_command, "say Hello!", true);
    That would have the same effect - it would wait 12000ms (two minutes), then call send_command(), passing it "say Hello!" and true.

    When you create a timeout, it returns a reference to it, so if you do what Antonius did and save that reference to a variable, you can clear it with clearTimeout(myTimeout);

    So if you did it Antonius's way, saving the reference (in case you want to stop the timer before it goes off) and using an anonymous function, it would look like this:
    let myTimeout = setTimeout(120000, function () {
        send_command("say Hello!", true);
    });
    let is like Antonius's var, but is a newer part of JavaScript that works the way you expect it to. var is older and has function scope, let has block scope and works the same way as local variables in Mudlet's Lua. Save yourself a future headache and just use let instead.

    setInterval works the same way, but fires at intervals instead of just once and has clearInterval instead of clearTimeout.

    One thing to be careful about is that you won't get exact timings. The way JavaScript works is that when the timer ends, it adds the function call to the end of a sort of to-do list. So if any other code is running or is about to run, it'll end up running before the function from your timer does. That probably won't matter, but if you're noticing that the timing isn't quite perfect, that's why.

    Also, if you want to do echoes, in Nexus they're display notices. They look like this:
    display_notice("This is some red text on a white background!", "red", "white");
    You can leave off the colors and it'll use defaults (if you want to leave off the fg color, but not the bg, just put "" for the fg).
    Aquil
Sign In to Comment.