Usage
nebula.once(fn, args, unique)
Parameters
fn
(Optional) (Function) The function to trigger one time only (without self-executing parenthesis)
Default: None
args
(Optional) (Array) The parameters for the function
Default: None
unique
(Required) (String) A unique string
Default: None
Parameter Notes
Call fn without self-executing parenthesis.
args should be an array of parameters. ['parameter1', 'parameter2']
is the equivalent of calling customFunction('parameter1', 'parameter2');
Call this function with only a unique string to return boolean whether that function has been run yet or not.
You can remove a single instance with once('remove', 'unique string here')
and you can reset all instances with once('reset')
Examples
Call with a function name and unique string.
nebula.once(customFunction, 'test example');
Call with a function name and two parameters.
nebula.once(customFunction, ['parameter1', 'parameter2'], 'test example');
Returns boolean true/false if this unique string has been triggered yet.
nebula.once('example test');
Remove a single instance
nebula.once('remove', 'example test');
Reset all instances
nebula.once('reset');
Additional Notes
This function stores all data in a window object called onces
.
Source File
Located in /assets/js/modules/utilities.js on line 445.
No Hooks
This function does not have any filters or actions available. Request one?nebula.once = function(fn, args, unique){ nebula.onces = nebula.onces || {}; if ( typeof args === 'string' ){ //If no parameters unique = args; args = []; } //Reset all if ( fn === 'clear' || fn === 'reset' ){ nebula.onces = {}; return true; } //Remove a single entry if ( fn === 'remove' ){ delete nebula.onces[unique]; return true; } // If the first parameter fn is a function, execute it only once if ( typeof fn === 'function' ){ if ( !nebula.onces[unique] ){ nebula.onces[unique] = true; return fn.apply(this, args); } return false; //Do nothing if already executed } //If the first parameter fn is not a function, assume it's the 'unique' identifier and return boolean if ( !nebula.onces[fn] ){ nebula.onces[fn] = true; return true; } return false; };
Override
To override or disable this JavaScript function, simply redeclare it with the exact same function name. Remember: Some functionality is conditionally loaded via dynamic imports, so if your function is not overriding properly, try listening for a DOM event (described below).
For non-module import functions:
nebula.once = function(fn, args, unique){ //Write your own code here, leave it blank, or return false. }
For dynamically imported module function overrides:
jQuery(window).on('load', function(){ nebula.once = function(fn, args, unique){ //Write your own code here, leave it blank, or return false. } });
Custom Nebula DOM events do also exist, so you could also try the following if the Window Load listener does not work:
jQuery(document).on('nebula_module_loaded', function(module){ //Note that the module variable is also available to know which module specifically was imported if ( typeof nebula.once === 'function' ){ nebula.once = function(fn, args, unique){ //Write your own code here, leave it blank, or return false. } } });