Skip to content

Instantly share code, notes, and snippets.

@mlshvdv
Created May 20, 2014 13:12
Show Gist options
  • Save mlshvdv/551ac4ed944e850d703f to your computer and use it in GitHub Desktop.
Save mlshvdv/551ac4ed944e850d703f to your computer and use it in GitHub Desktop.
Javascript anti-flood system
var score = 0;
var penalty = 100; // Penalty can be fine-tuned.
var lastact = new Date();
function talk() {
/* The smaller the distance, more time has to pass in order
* to negate the score penalty cause{d,s}.
*/
score -= (new Date() - lastact) * 0.55;
// Score shouldn't be less than zero.
score = (score < 0) ? 0 : score;
console.log("--------------");
console.log((score += penalty));
if ( parseInt( (score += penalty) ) <= 1000 ) {
// Do things.
console.log("Work");
} else {
console.log("Error");
}
lastact = new Date();
}
$(document).ready(function(){
$("form").submit(function(){
talk();
return false;
});
});
@felipemarques
Copy link

PERFECT!!! THANKYOU!!!

@melkiyprod
Copy link

Дмитрий, добрый вечер, а не подскажите как переделать его, чтобы считал не по очкам, а по времени. К примеру (одно сообщение = 1000, если больше одного сообщения в секунду - do nothing) . Спасибо)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment