Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Test
- {
- private var antiflood_stack:Array<Void->Void>;
- private var antiflood_timestamp:Float = 0;
- private var delay:Int;
- public function new(delay: Int) {
- this.delay = delay;
- antiflood_stack = new Array<Void->Void>();
- var antiflood_timer = new haxe.Timer(delay);
- antiflood_timer.run = function() {
- var callback = antiflood_stack.pop();
- if (callback != null){
- callback();
- antiflood_timestamp = Date.now().getTime();
- }
- }
- }
- static function main() {
- var antiflood = new Test(4000);
- var i = 0;
- antiflood.call(function() {
- trace('test ${i++}');
- });
- antiflood.call(function() {
- trace('test ${i++}');
- });
- antiflood.call(function() {
- trace('test ${i++}');
- });
- antiflood.call(function() {
- trace('test ${i++}');
- });
- }
- public function call(func: Void->Void)
- {
- if(antiflood_stack.length == 0 && (Date.now().getTime() - antiflood_timestamp) > delay)
- {
- func();
- antiflood_timestamp = Date.now().getTime();
- }
- else
- {
- antiflood_stack.insert(0, func);
- }
- }
- }
Add Comment
Please, Sign In to add comment