// Декоратор для функции action, не выполняющий ее чаще limit раз в секунду
function slowpoke(limit, action) {
var timeout = 1000 / limit;
var timer = null;
var self, args;
// Выполняет action, если требуется
function tryInvoke() {
// остановить таймер, если нечего выполнять
if (!args) {
clearInterval(timer);
timer = null;
return;
}
// выполнить action
action.apply(self, args);
// сбросить параметры вызова, чтобы освободить память и показать, что нечего выполнять
self = args = null;
}
return function () {
self = this;
args = Array.prototype.slice.call(arguments);
// если таймер запущен, поручить выполнение action ему
if (timer !== null) {
return;
}
// если нет, запустить таймер
timer = setInterval(tryInvoke, timeout);
// и выполнить action сейчас
tryInvoke();
};
}
// ================ тесты ================
describe('slowpoke', function () {
var originAction;
var slowpokeAction;
var n = 10;
beforeEach(function () {
jasmine.clock().install();
originAction = jasmine.createSpy('action');
slowpokeAction = slowpoke(n, originAction);
});
afterEach(function() {
jasmine.clock().uninstall();
});
describe('возвращает функцию, которая выполняется', function () {
it('синхронно при первом вызове', function () {
slowpokeAction();
expect(originAction.calls.count()).toBe(1);
});
it('дважды с интервалом 1/n секунд при 3х вызовах подряд', function () {
slowpokeAction();
slowpokeAction();
slowpokeAction();
expect(originAction.calls.count()).toBe(1);
jasmine.clock().tick(1000 / n - 1);
expect(originAction.calls.count()).toBe(1);
jasmine.clock().tick(1);
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.