[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Jscript
. . -> [Subject]  Randomly delaying in a script?

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Randomly delaying in a script?

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page


Posted by Metaxy   (28 posts)  [Biography] bio
Date Sat 16 Nov 2002 10:37 AM (UTC)  quote  ]

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);
}
[Go to top] top

Posted by Vaejor   (120 posts)  [Biography] bio
Date Reply #1 on Sat 16 Nov 2002 02:13 PM (UTC)  quote  ]
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");
}
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Sat 16 Nov 2002 09:05 PM (UTC)  quote  ]
Message
Or, more simply:

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

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

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

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Sat 16 Nov 2002 10:01 PM (UTC)  quote  ]
Message
Oh. Sorry, in that case yours is the better solution. :)

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Metaxy   (28 posts)  [Biography] bio
Date Reply #5 on Sun 17 Nov 2002 06:08 AM (UTC)  quote  ]
Message
Why has the setTimeout function been removed? Isn't that part of the specification for the language?
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Reply #6 on Sun 17 Nov 2002 07:33 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Doorie   Finland  (10 posts)  [Biography] bio
Date Reply #7 on Mon 14 Mar 2011 04:40 PM (UTC)  quote  ]
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.
[Go to top] top

Posted by Twisol   USA  (2,229 posts)  [Biography] bio
Date Reply #8 on Mon 14 Mar 2011 09:08 PM (UTC)  quote  ]
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
[Go to top] top

Posted by Twisol   USA  (2,229 posts)  [Biography] bio
Date Reply #9 on Mon 14 Mar 2011 09:11 PM (UTC)  quote  ]
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
[Go to top] 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.


4,126 views.

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]