A free plan allows three exports. A tooltip explains a feature the first two times and then stays quiet. A retry button may try five times. Each is a function that is allowed to run only a fixed number of times, after which calling it changes nothing.
Write limitCalls(fn, n) in limit.js. It returns a new function. The first n calls of that function run fn and return its result. Every call after that returns the result of the last call that did run fn, without running it.
n: how many calls may run fn. A fraction is cut down toward zero (2.7 allows 2 calls). Zero, a negative number, NaN or a missing n allow none. Infinity allows every call.
The returned function returns fn's result, or the last result once the limit is reached (undefined if fn never ran).
limitCalls returns a function and does not call fn itself.
Each of the first n calls runs fn with that call's arguments and this, and returns its result.
Once n calls have run fn, later calls do not run it and return the last result, even when that result is falsy.
n is cut to a whole number toward zero; 0, negative, NaN and missing mean fn never runs; Infinity means no limit.
A call counts as soon as it starts: a call where fn throws uses up one of the n (and the error reaches the caller), and so does a call made from inside fn.
let exports =0;constexportFile=limitCalls(() =>++exports, 3);exportFile(); // 1exportFile(); // 2exportFile(); // 3exportFile(); // 3: fn did not run, the last result again