所以我这里有这段代码:
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);
我希望能够让它有某种冷却,所以它不能连续快速使用。我想要这个的原因是为了防止人们被跳过,因为这就是这段代码用于跳过人的原因。
我希望这些信息足以获得一些帮助。谢谢!
最佳答案
您可以使用下划线的 debounce()
方法(以 true
作为第三个参数)。
如果您不想为这个简单的任务包含 Underscore,您可以...
var debounceFn = function (fn, delay) {
var lastInvocationTime = Date.now();
delay = delay || 0;
return function () {
(Date.now() - delay > lastInvocationTime) && (lastInvocationTime = Date.now()) && fn && fn();;
};
};
jsFiddle .
What I need is a way to not be able to execute the command more than once in a row.
你可以做类似的事情......
var onceFn = function (fn) {
var invoked = false;
return function () {
! invoked && (invoked = true) && fn && fn();
};
};
jsFiddle .
关于javascript - 我不想很快地运行同一个命令两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17422274/