Release
This commit is contained in:
57
client/islets/core/y-throttle/y-throttle.js
Normal file
57
client/islets/core/y-throttle/y-throttle.js
Normal file
@@ -0,0 +1,57 @@
|
||||
modules.define('y-throttle', function (provide) {
|
||||
/**
|
||||
* Возвращает новую функцию, которая при повторных вызовах,
|
||||
* вызывает функцию func не чаще одного раза в заданный
|
||||
* промежуток wait.
|
||||
*
|
||||
* Полезна для использования при обработке событий, которые
|
||||
* происходят слишком часто.
|
||||
*
|
||||
* @name throttle
|
||||
* @param {Function} func
|
||||
* @param {Number} wait Минимальный промежуток времени в миллисекундах,
|
||||
* который должен пройти между вызовами func.
|
||||
* @param {Object} [options]
|
||||
* @param {Boolean} [options.leading=true] Включает исполнение функции вначале.
|
||||
* @param {Boolean} [options.trailing=true] Включает исполнение функции вконце.
|
||||
* @returns {Function}
|
||||
*
|
||||
* @example
|
||||
* var updatePosition = function () {};
|
||||
* var throttled = throttle(updatePosition, 100);
|
||||
* $(window).scroll(throttled);
|
||||
*/
|
||||
provide(function (func, wait, options) {
|
||||
var context;
|
||||
var args;
|
||||
var result;
|
||||
var timeout = null;
|
||||
var previous = 0;
|
||||
options = options || {};
|
||||
|
||||
var later = function () {
|
||||
previous = options.leading === false ? 0 : Date.now();
|
||||
timeout = null;
|
||||
result = func.apply(context, args);
|
||||
};
|
||||
|
||||
return function () {
|
||||
var now = Date.now();
|
||||
if (!previous && options.leading === false) {
|
||||
previous = now;
|
||||
}
|
||||
var remaining = wait - (now - previous);
|
||||
context = this;
|
||||
args = arguments;
|
||||
if (remaining <= 0) {
|
||||
clearTimeout(timeout);
|
||||
timeout = null;
|
||||
previous = now;
|
||||
result = func.apply(context, args);
|
||||
} else if (!timeout && options.trailing !== false) {
|
||||
timeout = setTimeout(later, remaining);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
});
|
||||
});
|
||||
7
client/islets/core/y-throttle/y-throttle.md
Normal file
7
client/islets/core/y-throttle/y-throttle.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# y-throttle:
|
||||
|
||||
Модуль `y-throttle` возвращает функцию `throttle`,
|
||||
которая ограничивает количество выполненных действий в заданном интервале времени.
|
||||
Подробности по клику на функции.
|
||||
|
||||
<!--JS_API-->
|
||||
88
client/islets/core/y-throttle/y-throttle.test.js
Normal file
88
client/islets/core/y-throttle/y-throttle.test.js
Normal file
@@ -0,0 +1,88 @@
|
||||
modules.define('test', ['y-throttle'], function (provide, throttle) {
|
||||
|
||||
describe('throttle', function () {
|
||||
it('should throttle given function', function (done) {
|
||||
var res = [];
|
||||
var throttledFn = throttle(function (arg) {
|
||||
res.push(arg);
|
||||
}, 20);
|
||||
|
||||
throttledFn(1);
|
||||
throttledFn(2);
|
||||
throttledFn(3);
|
||||
|
||||
setTimeout(function () {
|
||||
throttledFn(4);
|
||||
}, 10);
|
||||
|
||||
setTimeout(function () {
|
||||
throttledFn(5);
|
||||
res.should.deep.eq([1, 4]);
|
||||
done();
|
||||
}, 30);
|
||||
});
|
||||
|
||||
it('should not trigger leading call when option "leading" is set to false', function (done) {
|
||||
var res = [];
|
||||
var throttledFn = throttle(function (arg) {
|
||||
res.push(arg);
|
||||
}, 20, {leading: false});
|
||||
|
||||
throttledFn(1);
|
||||
throttledFn(2);
|
||||
throttledFn(3);
|
||||
|
||||
setTimeout(function () {
|
||||
throttledFn(4);
|
||||
}, 10);
|
||||
|
||||
setTimeout(function () {
|
||||
throttledFn(5);
|
||||
res.should.deep.eq([4]);
|
||||
done();
|
||||
}, 30);
|
||||
});
|
||||
|
||||
it('should not trigger trailing call when option "trailing" is set to false', function (done) {
|
||||
var res = [];
|
||||
var throttledFn = throttle(function (arg) {
|
||||
res.push(arg);
|
||||
}, 20, {trailing: false});
|
||||
|
||||
throttledFn(1);
|
||||
throttledFn(2);
|
||||
throttledFn(3);
|
||||
|
||||
setTimeout(function () {
|
||||
throttledFn(4);
|
||||
}, 10);
|
||||
|
||||
setTimeout(function () {
|
||||
res.should.deep.eq([1]);
|
||||
done();
|
||||
}, 30);
|
||||
});
|
||||
|
||||
it('should not trigger leading and trailing calls when both options are set to false', function (done) {
|
||||
var res = [];
|
||||
var throttledFn = throttle(function (arg) {
|
||||
res.push(arg);
|
||||
}, 20, {leading: false, trailing: false});
|
||||
|
||||
throttledFn(1);
|
||||
throttledFn(2);
|
||||
throttledFn(3);
|
||||
|
||||
setTimeout(function () {
|
||||
throttledFn(4);
|
||||
}, 10);
|
||||
|
||||
setTimeout(function () {
|
||||
res.should.deep.eq([]);
|
||||
done();
|
||||
}, 30);
|
||||
});
|
||||
});
|
||||
|
||||
provide();
|
||||
});
|
||||
Reference in New Issue
Block a user