Returns a proportion of amount depending on elapsed time.
Cumulatively, amount is yielded every second.
// Calculate a proportion of 0.1 every second constx = perSecond(0.1); x();
The faster x() is called, the smaller the chunks of amount are returned.
Values accumulate. For example, x() isn't called for two seconds, 2*amount is returned.
Example: Usage
constsettings = { ageMod:perSecond(0.1); };
letstate = { age:1 };
// Update setInterval(() => { let { age } = state; // Add 0.1 per second, regardless of // loop speed age += settings.ageMod(); state = { ...state, age:clamp(age) } });
Use the clamp option so the returned value never exceeds amount.
Alternatively, min/max options allow you to set arbitrary limits.
Returns a proportion of
amount
depending on elapsed time. Cumulatively,amount
is yielded every second.The faster
x()
is called, the smaller the chunks ofamount
are returned. Values accumulate. For example,x()
isn't called for two seconds, 2*amount is returned.Example: Usage
Use the
clamp
option so the returned value never exceedsamount
. Alternatively,min
/max
options allow you to set arbitrary limits.