Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6038837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:16:50+00:00 2026-05-23T06:16:50+00:00

I’m having trouble figuring out how to handle multiple instances of some javascript functions

  • 0

I’m having trouble figuring out how to handle multiple instances of some javascript functions that I want to run on a page. It’s part of a custom analytics project that I’m working on.

I have a function called initData(); The function uses a setInterval to call another function that sends a ping to my server every 1000ms.

The problem is that I want to be able to have more than one instance of this function on a single page. My current problem is that as soon as the second instance is called it overwrites all the variables from the first.

What’s the best way to get around this? Is there a way to make the functions seperate and/or private instances so that they don’t interfere with each other?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:16:51+00:00Added an answer on May 23, 2026 at 6:16 am

    By default all variables (and therefore also function declarations) live in the global namespace.

    The only way to introduce a separate namespace in javascript is with a function call. This is how you do that:

     (function () {
         /* your stuff here */
     }());
    

    You wrap your stuff in an anonymous function, and then call it immediately. This way your functions will be separate, even with the same name.

    Example

    So, for instance, if you have code like this:

    var my, local, data;
    
    function initData() {
        // use my, local and data here.
    }
    

    and you have someplace else:

    var some, other, data;
    
    function initData() {
        // use some, other, data here.
    }
    

    then one initData will overwrite the other initData. However, if you wrap each in its own (function () {}()); then they will be separate.

    (function () {
        var my, local, data;
    
        function initData() {
            // use my, local and data here.
        }
    }());
    
    
    (function () {
        var some, other, data;
    
        function initData() {
            // use some, other, data here.
        }
    }());
    

    Be aware though, that these names are no longer in the global namespace, so they are also not available for use outside of the anonymous function.

    One global

    To control how much and what you expose in the global namespace, it is custom to expose what you need through one global object, usually in all capital letters.

    FOO.module1.initData();
    FOO.module2.initData();
    

    You would do this by making sure FOO exists, and if it doesn’t: create it.

    var FOO = this.FOO || {};
    

    The same for your module namespaces:

    FOO.module1 = FOO.module1 || {};
    

    and then inside of the anonymous function, expose what you want.

    Complete example

    module1.js:

    var FOO = this.FOO || {};
    FOO.module1 = FOO.module1 || {};
    
    (function () {
        var my, local, data;
    
        function initData() {
            // use my, local and data here.
        }
    
        FOO.module1.initData = initData;
    }());
    

    module2.js:

    var FOO = this.FOO || {};
    FOO.module2 = FOO.module2 || {};
    
    (function () {
        var some, other, data;
    
        function initData() {
            // use some, other and data here.
        }
    
        FOO.module2.initData = initData;
    }());
    

    controller.js:

    FOO.module1.initData();
    FOO.module2.initData();
    

    A last tip

    The controller like written is dependent on both module1.js and module2.js and needs to be loaded last. That can be avoided using something like jQuery‘s document.ready, making it wait for all scripts to load.

    jQuery(document).ready(function () {
        FOO.module1.initData();
        FOO.module2.initData();
    });
    

    If you’re not already using jQuery, you can use a small script like domReady to avoid the bloat.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.