Namespace: Polling:poll

Polling:poll

Source:

Methods

poll(name, fCondition, fAction, fElseActionopt, nullable, newLoopMsopt, nullable, useOwnTimeropt, nullable) → {Object}

Waits for a certain program- or DOM-state before executing a certain action. Waiting is implemented via a global timer (and optionally locals as well). If you need to react to a certain case, that's not defined by standard events and reaction does not have to be razor sharp, this is your method. Pick a name for the state/event you want to poll, define a condition closure and an action closure that holds what is to be done in case the condition works out. Polls end or are repeated after an execution of the action depending on the result of the action closure. There can always be only one poll of a certain name, redefining it overwrites the first one.

If you need to evaluate a poll out of the line, to trigger a sharp synchronous evaluation due to an event for example, you can use the "fire" method of the poll object itself, which will trigger the condition and all subsequent actions. You can provide a boolean parameter to this function to override manually if the result should be considered changed to the last run. You can only fire polls, that are still active, you can check this state easily via "isActive" on the poll.

Parameters:
Name Type Attributes Default Description
name String

name of the state or event you are waiting/polling for

fCondition function

closure to define the state to wait for, returns true if state exists and false if not

fAction function

closure to define action to take place if condition is fulfilled, poll removes itself if this evaluates to true, receives Boolean parameter defining if condition result has changed since last call

fElseAction function <optional>
<nullable>
null

closure to define action to take place if condition is not fulfilled, receives Boolean parameter defining if condition result has changed since last call

newLoopMs Number <optional>
<nullable>
250

sets interval length from here on, resets global loop if useOwnTimer is not set, otherwise sets local interval for that poll poll

useOwnTimer Boolean <optional>
<nullable>
false

has to be true to tell the poll to use an independent local timer instead of the global one, use this if you need different levels of fuzziness for you polls, performance-wise it's better to have less independent intervals running

Source:
See:
Throws:

error in case name, fCondition or fAction are missing or unfit to use

Returns:
new poll - structure: {name, condition, action, elseAction, loop, lastPollResult, isActive, fire()}
Type
Object
Example
const pollBodyHeightPermanently = poll('permanent-body-height-poll', function(){ return document.body.scrollHeight > 1000; }, function(changed){ console.log(`too high${changed ? ' as of yet' : ''}!`); }, null, 5000);
const pollBodyHeightAndStopIfHighEnough = poll('one-time-body-height-poll', function(){ return document.body.scrollHeight > 1000; }, function(){ console.log('high enough!'); return true; }, function(){ console.log('not high enough yet :(') }, null, true);