Skip to Content
Menu

once()

Allows functions to be triggered once per pageload.

JavaScript February 7, 2021

Usage

JavaScript
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')

Request or provide clarification »

Examples

Call with a function name and unique string.

JavaScript
nebula.once(customFunction, 'test example');

Call with a function name and two parameters.

JavaScript
nebula.once(customFunction, ['parameter1', 'parameter2'], 'test example');

Returns boolean true/false if this unique string has been triggered yet.

JavaScript
nebula.once('example test');

Remove a single instance

JavaScript
nebula.once('remove', 'example test');

Reset all instances

JavaScript
nebula.once('reset');

Additional Notes

This function stores all data in a window object called onces.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/utilities.js on line 404.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.once = function(fn, args, unique){
        if ( typeof nebula.onces === 'undefined' ){
            nebula.onces = {};
        }
    
        if ( typeof args === 'string' ){ //If no parameters
            unique = args;
            args = [];
        }
    
        //Reset all
        if ( fn === 'clear' || fn === 'reset' ){
            nebula.onces = {};
        }
    
        //Remove a single entry
        if ( fn === 'remove' ){
            delete nebula.onces[unique];
        }
    
        if ( typeof fn === 'function' ){ //If the first parameter is a function
            if ( typeof nebula.onces[unique] === 'undefined' || !nebula.onces[unique] ){
                nebula.onces[unique] = true;
                return fn.apply(this, args);
            }
        } else { //Else return boolean
            unique = fn; //If only one parameter is passed
            if ( typeof nebula.onces[unique] === 'undefined' || !nebula.onces[unique] ){
                nebula.onces[unique] = 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).

    JavaScript

    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.
            }
    	}
    });