Returns a proportion of amount depending on elapsed time. Cumulatively, amount is yielded every second.

// Calculate a proportion of 0.1 every second
const x = 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.

const settings = {
ageMod: perSecond(0.1);
};

let state = {
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.

  • Parameters

    • amount: number
    • options: Partial<{
          clamp: boolean;
          max: number;
          min: number;
      }> = {}

    Returns ModSource