function throttle(fn) {
let timer;
let status = false;
return function(...args) {
const context = this;
clearTimeout(timer);
if (status) return;
fn(context, args);
};
let count = 0;
const init = once((a, b) => {
count++;
return a + b;
});
}
init(1, 2); // 3
init(5, 5); // 3 ← не 10
init(); // 3
count; // 1
const obj = {
name: 'test',
getName: once(function() {
return this.name;
}),
};
obj.getName(); // 'test'
Code will run automatically after changes.
Code runs in an isolated environment.
Code runs in an isolated environment.