Multiple function calls allow only one execution
function once(fn) {
let result;
return function () {
if (fn) {
result = fn.apply(null.arguments);
// eslint-disable-next-line no-param-reassign
fn = function () {};
}
return result;
};
}
let run = once((str) = > {
console.log(str);
});
run(1);
run(2);
run(3);
run(4);
Copy the code