Randomly delaying in a script?

Posted by Metaxy on Sat 16 Nov 2002 10:37 AM — 10 posts, 47,007 views.

#0
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);
}
Amended on Sat 16 Nov 2002 10:39 AM by Metaxy
#1
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");
}
Australia Forum Administrator #2
Or, more simply:

world.DoAfter (Math.floor(Math.random() * 8) + 1, "look");
#3
Seemed there was some interest in causing it to be repetative instead of just a single occurance.
Australia Forum Administrator #4
Oh. Sorry, in that case yours is the better solution. :)
#5
Why has the setTimeout function been removed? Isn't that part of the specification for the language?
Australia Forum Administrator #6
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.
Finland #7
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.
USA #8
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
USA #9
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.