new DeferValue(init)
Accepts a function to initialize the value of a variable when idle.
Parameters:
Name | Type | Description |
---|---|---|
init |
function |
Example
const formatter = new DeferValue(() => {
return new Intl.DateTimeFormat('en-US', {
timeZone: 'America/Los_Angeles',
});
});
console.log(this.formatter.getValue().format(new Date()));
Note: If you’re in a situation where you want to use the DeferValue class but you can’t change your public API,
you can use the DeferValue with getters or use `defineDeferProp`
Methods
-
getValue() → {?}
-
Returns the value if it's already been initialized. If it hasn't then the initializer function is run immediately and the pending idle callback is cancelled.
Returns:
? - depends on the init function -
setValue(newValue)
-
Cancels any scheduled requestIdleCallback and sets the new value as defer value
Parameters:
Name Type Description newValue
? value for the DeferValue