Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Jscript ➜ Randomly delaying in a script?

Randomly delaying in a script?

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Metaxy   (28 posts)  Bio
Date Sat 16 Nov 2002 10:37 AM (UTC)

Amended on Sat 16 Nov 2002 10:39 AM (UTC) by Metaxy

Message
How do I randomly delay? Say I want to send a command at random intervals between 1 and 8 seconds, how would I attain that? I tried using the setTimeout() function that you use when using J(ava)Script for client-side web scripting, but it fails with an "object expected" error. Here's the code I'm using:
function doSend()
{
world.send("look");
setTimeout("doSend()", 1000);
}
Top

Posted by Vaejor   (120 posts)  Bio
Date Reply #1 on Sat 16 Nov 2002 02:13 PM (UTC)
Message
It's been a while since I've done any useful jscript, but here's how I think it would look. It almost definately has errors in it, but a starting point at least.

function doSend() {
  world.send("look");
  world.AddTimer("relook", 0, 0, Math.floor(Math.random() * 8) + 1, "", 1+2+4+1024+16384, "doSend");
}
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #2 on Sat 16 Nov 2002 09:05 PM (UTC)
Message
Or, more simply:

world.DoAfter (Math.floor(Math.random() * 8) + 1, "look");

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Vaejor   (120 posts)  Bio
Date Reply #3 on Sat 16 Nov 2002 09:51 PM (UTC)
Message
Seemed there was some interest in causing it to be repetative instead of just a single occurance.
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #4 on Sat 16 Nov 2002 10:01 PM (UTC)
Message
Oh. Sorry, in that case yours is the better solution. :)

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Metaxy   (28 posts)  Bio
Date Reply #5 on Sun 17 Nov 2002 06:08 AM (UTC)
Message
Why has the setTimeout function been removed? Isn't that part of the specification for the language?
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #6 on Sun 17 Nov 2002 07:33 AM (UTC)
Message
I cannot find setTimeout in the JScript documentation except on one page where it is used by example.

Remember, this is Jscript not Java. I have not disabled setTimeout, if the script engine does not support it then I can't help that.

I doubt that it would support that sort of thing. MUSHclient calls scripts in well-defined places, I can't see how it can be expected to allow for a script to suddenly "kick back in" a second later.

The methods we have described will solve your problem. If you can find documentation about how setTimeout is supposed to work in the JScript language (not Java) then please supply the appropriate URL.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Doorie   Finland  (10 posts)  Bio
Date Reply #7 on Mon 14 Mar 2011 04:40 PM (UTC)
Message
Nick Gammon said:

Or, more simply:

world.DoAfter (Math.floor(Math.random() * 8) + 1, "look");



Above is jscript? How does one do it in Lua?
(In Edit trigger box, in 'Send:' textbox.)

Does it include possible numbers like 0.39sec, 1.5sec, 3,14159sec, 8.54sec ... ie. how accurate is the randomness?

Thank you in advance! This would solve an _old_ problem for me.
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #8 on Mon 14 Mar 2011 09:08 PM (UTC)
Message
SSanttu said:
Nick Gammon said:

Or, more simply:

world.DoAfter(Math.floor(Math.random() * 8) + 1, "look");

Above is jscript? How does one do it in Lua?

DoAfter (math.floor(math.random() * 8) + 1, "look")


Ssanttu said:
Does it include possible numbers like 0.39sec, 1.5sec, 3,14159sec, 8.54sec ... ie. how accurate is the randomness?

Hit Ctrl+I in MUSHclint to pop open a script entry window, and type in something like Note(math.random()) to see. math.random() returns random real numbers in the interval [0, 1), so the above code multiplies it by 8 to get an interval of roughly [0, 8). But then it's passed through math.floor(), lopping off the decimal part (giving us [0, 7]). If you don't want that, just remove the math.floor() invocation.

The +1 afterwrads just increments the interval: [1, 8].

Interval notation: http://zonalandeducation.com/mmts/miscellaneousMath/intervalNotation/intervalNotation.html

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #9 on Mon 14 Mar 2011 09:11 PM (UTC)
Message
Sorry, I just have to respond to this, even though it's a decade old.
Nick Gammon said:
I cannot find setTimeout in the JScript documentation except on one page where it is used by example.

Remember, this is Jscript not Java. I have not disabled setTimeout, if the script engine does not support it then I can't help that.

I doubt that it would support that sort of thing. MUSHclient calls scripts in well-defined places, I can't see how it can be expected to allow for a script to suddenly "kick back in" a second later.

The methods we have described will solve your problem. If you can find documentation about how setTimeout is supposed to work in the JScript language (not Java) then please supply the appropriate URL.

setTimeout is actually defined by DOM Level 0, which is a browser API used by Javascript. The DOM API isn't part of the Javascript language, it's something that browsers expose. As MUSHclient isn't a browser, it doesn't support the DOM API.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


36,356 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.