- Home /
.js Timer to prevent chatflood?
Settin' up a school interactive chatroom, and wanted to prevent flooding of the RPC, how do I create in javascript a timer, that functions as a cooldown on your chat, e.g. having at least 1½ seconds between posts at best?
Answer by Bunny83 · Aug 12, 2012 at 02:32 AM
It depends on how the chat works. If you have an authoriative server the check has to be implemented only on the server but it can't be removed (or bypassed). If you have a "normal" network setup, so each client send the rpc to all others itself you can implement it on the sending or receiving part. The easiest way is in the sending part but that could be bypassed. The other way would be that each client holds a timer for each other peer and refuses the receiving when it's too frequent.
The usual way is to use a simple float variable that stores the timestamp of the last message. You can simply check when the last one has been sent / received and refuse sending / receiving when it's faster than the allowed time.
Example:
// UnityScript:
public var cooldown : float = 1.5
private var lastTimeStamp : float = 0.0;
function SendMessage(text : String)
{
if(Time.time > lastTimeStamp + cooldown)
{
networkView.RPC(/*Send your message here*/);
lastTimeStamp = Time.time;
}
}
Someone please change this to an answer, it worked almost perfectly, just had to change 'time.time' to 'Time.time' but else than that, the idea of storing the time of last message, and checking if the new message is over the old timer + cooldown worked perfectly. Another solved unity thread, for people to find help from in the future.
I'm a bit confused. This is an answer ;) And yes, my shift key sometimes won't work, sorry i will fix it. I will also add a cooldown variable since magic numbers are bad in general ;)
Your answer
Follow this Question
Related Questions
how to make a button click able after a few seconds 2 Answers
Usage timer/weapon cooldown... 3 Answers
Very Very Precise Timer ? 0 Answers
How to add a cooldown sort of thing. 1 Answer
C# Weapon overheat Buildup/Cooldown 1 Answer