Module: rgjs/schedule

RGJS6 Schedule module.

Methods


<static> beforeNextPaint(func [, id])

Execute before next paint.
Note: will automatically cancel ongoing requests if the id matches.

Parameters:
Name Type Argument Description
func function

The function to be called.

id string <optional>

Optional. Unique id for the request.

Returns:

A unique identifier (can be used to cancel the request).

Type
string
Examples
// Request the *next* animation frame and store auto-generated id.
const bnp_id = rgjs.schedule.beforeNextPaint(callback);
// Request the *next* animation frame and use a custom id.
rgjs.schedule.beforeNextPaint(callback, 'custom_id');

<static> beforePaint(func [, id])

Execute before paint (requestAnimationFrame()).
Note: will automatically cancel scheduled calls if the id matches.

Parameters:
Name Type Argument Description
func function

The function to be called.

id string <optional>

Optional. Unique id for the request.

Returns:

A unique identifier (can be used to cancel the request).

Type
string
Examples
// Request an animation frame and store auto-generated id.
const bp_id = rgjs.schedule.beforePaint(callback);
// Request an animation frame and use a custom id.
rgjs.schedule.beforePaint(callback, 'custom_id');

<static> beforePaintLoop(func [, id])

Create a loop, each execution before paint (requestAnimationFrameLoop()).
Note: will automatically cancel ongoing loops if the id matches.

Parameters:
Name Type Argument Description
func function

The function to be called. A new frame will be requested as long as this function returns true.

id string <optional>

Optional. Unique id for the loop.

Returns:

A unique identifier (can be used to cancel the loop).

Type
string
Example
// Run a loop for ~1000 ms and report how many frames were fired in that time.
const time_bgn = Date.now();
const time_end = time_bgn + 1000;
let frames = 0;
const fn = () => {
  frames++;
  const finished = ;
  if ((time_end < new Date().geTime())) {
    console.log(frames +' frame(s)');
    return false; // causes the loop to stop.
  }
  return true; // causes the loop to continue.
};
rgjs.schedule.beforePaintLoop(fn);

<static> cancelBeforePaint(id)

Cancel a beforePaint() or beforeNextPaint() call by id (cancelRequestAnimationFrame()).

Parameters:
Name Type Description
id string

The id of the request.

Returns:

True on success, false if request was not found.

Type
boolean
Examples
// Request an animation frame and store auto-generated id, then cancel it.
const bp_id = rgjs.schedule.beforePaint(callback);
rgjs.schedule.cancelBeforePaint(bnp_id);
// Request the *next* animation frame and store auto-generated id, then cancel it.
const bnp_id = rgjs.schedule.beforeNextPaint(callback);
rgjs.schedule.cancelBeforePaint(bnp_id);

<static> cancelBeforePaintLoop(id)

Cancel a beforePaintLoop() call by id (cancelRequestAnimationFrame()).

Parameters:
Name Type Description
id string

The id of the loop.

Returns:

True on success, false if loop was not found.

Type
boolean
Example
// Create a loop, then cancel it.
const bpl_id = rgjs.schedule.beforePaintLoop(fn);
rgjs.schedule.cancelBeforePaintLoop(bpl_id);

<static> clearInterval(id)

Clear a timeout.

Parameters:
Name Type Description
id string

The id of the interval.

Returns:

True on success, false if interval was not found.

Type
boolean
Examples
// Set an interval and store auto-generated id, then cancel it.
const timeout_id = rgjs.schedule.setInterval(callback, 100);
rgjs.schedule.clearInterval(timeout_id);
// Set an interval and use a custom id, then cancel it.
rgjs.schedule.setInterval(callback, 100, 'custom_id');
rgjs.schedule.clearInterval('custom_id');

<static> clearTimeout(id)

Clear a timeout.

Parameters:
Name Type Description
id string

The id of the timeout.

Returns:

True on success, false if timeout was not found.

Type
boolean
Examples
// Set a timeout and store auto-generated id, then cancel it.
const timeout_id = rgjs.schedule.setTimeout(callback, 100);
rgjs.schedule.clearTimeout(timeout_id);
// Set a timeout and use a custom id, then cancel it.
rgjs.schedule.setTimeout(callback, 100, 'custom_id');
rgjs.schedule.clearTimeout('custom_id');

<static> debounce(func, time)

Debounce a function call.

Parameters:
Name Type Description
func function

The function to be called.

time int

The time in ms before the function is called.

Example
// Debounce scroll events to occur 100ms after scroll has settled.
window.addEventListener('scroll', debounce( event => {
  console.log(event);
}, 100));

<static> setInterval(func, time [, id] [, immediately])

Set an interval and fire it once, immediatly.
Note: will automatically cancel ongoing intervals if the id matches.

Parameters:
Name Type Argument Default Description
func function

The function to be called.

time int

The time in ms of the interval.

id string <optional>

Optional. Unique id for the interval.

immediately boolean <optional>
true

Optional. Defaults to true.

Returns:

A unique identifier (can be used to cancel the interval).

Type
string
Examples
// Set an interval and store auto-generated id.
// Note: fires immediately, then every 100ms.
const interval_id = rgjs.schedule.setInterval(callback, 100);
// Set an interval that does *not* fire immediately and store auto-generated id.
const interval_id = rgjs.schedule.setInterval(callback, 100, null, false);
// Set an interval and use a custom id.
// Note: fires immediately, then every 100ms.
rgjs.schedule.setInterval(callback, 100, 'custom_id');

<static> setTimeout(func, time [, id])

Set a timeout.
Note: will automatically cancel ongoing timeouts if the id matches.

Parameters:
Name Type Argument Description
func function

The function to be called.

time int

The time in ms before the function is called.

id string <optional>

Optional. Unique id for the timeout.

Returns:

A unique identifier (can be used to cancel the timeout).

Type
string
Examples
// Set a timeout and store auto-generated id
const timeout_id = rgjs.schedule.setTimeout(callback, 100);
// Set a timeout and use a custom id
rgjs.schedule.setTimeout(callback, 100, 'custom_id');

<static> throttle(func, time)

Throttle a function call.

Parameters:
Name Type Description
func function

The function to be called.

time int

The time in ms before the function is called.

Example
// Throttle scroll events to occur at a max of once per 100ms
window.addEventListener('scroll', throttle( event => {
  console.log(event);
}, 100));