Возвращает “обесшумленную функцию”. Функция будет вызвана после последнего вызова через объявленный таймаут.
Если задан флаг immediate
, функция вызовется немедленно, но будет игнорировать вызовы в пределах таймаута.
franky.debounce = function (func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) {
func.apply(context, args);
}
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) {
func.apply(context, args);
}
};
};
franky.Set = function () {
this.items = [];
};
franky.Set.prototype = {
add: function (value) {
if (!this.has(value)) {
this.items.push(value);
}
return this;
},
has: function (value) {
return x.indexOf(this.items, value) !== -1;
},
remove: function (value) {
var res;
if (this.has(value)) {
res = this.items.splice(
x.indexOf(this.items, value),
1
);
}
return res;
},
removeFirst: function () {
var res;
if (!this.isEmpty()) {
res = this.items.splice(0,1);
}
return res;
},
iterate: function (callback) {
for (var i = 0, l = this.items.length; i < l; i++) {
callback(this.items[i], i, this.items);
}
return this;
},
size: function () {
return this.items.length;
},
isEmpty: function () {
return this.items.length === 0;
}
};