I want it to be impossible to execute the same command twice very quickly

So, I have this piece of code:

lockskipCommand = (function(_super) { __extends(lockskipCommand, _super); function lockskipCommand() { return lockskipCommand.__super__.constructor.apply(this, arguments); } lockskipCommand.prototype.init = function() { this.command = '/lockskip'; this.parseType = 'exact'; return this.rankPrivelege = 'bouncer'; }; lockskipCommand.prototype.functionality = function() { data.lockBooth(); new ModerationForceSkipService(); return setTimeout((function() { return data.unlockBooth(); }), 4500); }; return lockskipCommand; })(Command); 

I want it to have something like cooling, so it cannot be used quickly in a row. The reason I want this is to prevent people from skipping because it is because this piece of code is for people skipping.

Hope this is enough to get some help. Thanks!

+4
source share
1 answer

You can use the Underscore debounce() method (with true as the third argument).

If you do not want to include Underscore for this simple task, you can do ...

 var debounceFn = function (fn, delay) { var lastInvocationTime = Date.now(); delay = delay || 0; return function () { (Date.now() - delay > lastInvocationTime) && (lastInvocationTime = Date.now()) && fn && fn();; }; }; 

jsFiddle .

I need a way to not execute a command more than once in a row.

You can do something like this ...

 var onceFn = function (fn) { var invoked = false; return function () { ! invoked && (invoked = true) && fn && fn(); }; }; 

jsFiddle .

+2
source

Source: https://habr.com/ru/post/1489246/


All Articles